Erlang: include module and call functions

☆樱花仙子☆ 提交于 2019-12-23 19:45:48

问题


I am going through Erlang code.

tes_lib:check_operational(Config)

The above code is present in a module called Sample.erl.

I am new to this language. My question is that I cannot see any include statement for the module tes_lib in Sample.erl. So, how come Sample.erl is able to call the function check_operational using tes_lib module?

I thought that it should be like Java, where we first import the class and then call the function.


回答1:


In Erlang, you don't need to "import" modules in order to be able to call them. A call like tes_lib:check_operational(Config) will be resolved at runtime. If the tes_lib module hasn't been loaded yet, the code server will look for it in the load path, and if the module can't be found, the call will fail with an undef error.


There is an -import directive in Erlang, but it's usually considered bad style to use it. You could write:

-import(tes_lib, [check_operational/1]).

and then call check_operational as if it were a local function, without specifying the module name. Those function calls will be replaced by fully qualified calls at compile time.

From the Erlang Programming Rules:

Don't use -import, using it makes the code harder to read since you cannot directly see in what module a function is defined. Use exref (Cross Reference Tool) to find module dependencies.



来源:https://stackoverflow.com/questions/36091757/erlang-include-module-and-call-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!