且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Erlang:包含模块和调用函数

更新时间:2022-06-27 00:20:17

在Erlang中,您无需导入"模块即可调用它们.像tes_lib:check_operational(Config)这样的调用将在运行时解决.如果尚未加载tes_lib模块,则代码服务器将在加载路径中查找该模块,如果找不到该模块,则调用将失败,并显示undef错误.

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.

在Erlang中有一个 指令,但是使用它通常被认为是不好的样式.您可以这样写:

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]).

,然后调用check_operational,就好像它是本地函数一样,而无需指定模块名称.这些函数调用将在编译时由完全合格的调用替换.

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.

从Erlang 编程规则:

不要使用-import,使用它会使代码更难阅读,因为您无法直接看到在哪个模块中定义了函数.使用exref(跨参考工具)查找模块依赖性.

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.