Erlang:包含模块和调用函数 [英] Erlang: include module and call functions

查看:249
本文介绍了Erlang:包含模块和调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看Erlang代码.

I am going through Erlang code.

tes_lib:check_operational(Config)

上面的代码存在于名为Sample.erl的模块中.

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

我是这门语言的新手.我的问题是在Sample.erl中看不到模块tes_lib的任何include语句.那么,Sample.erl如何能够使用tes_lib模块调用函数check_operational?

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?

我认为应该像Java,首先导入类,然后调用函数.

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

推荐答案

在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.

这篇关于Erlang:包含模块和调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆