gpt4 book ai didi

module - Erlang:包含模块和调用函数

转载 作者:行者123 更新时间:2023-12-02 18:20:26 25 4
gpt4 key购买 nike

我正在查看 Erlang 代码。

tes_lib:check_operational(Config)

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

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

我觉得应该像Java一样,先导入类,然后调用函数。

最佳答案

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



<小时/>

Erlang 中有一个 -import 指令,但通常认为使用它是不好的风格。你可以这样写:



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

然后调用 check_operational ,就好像它是本地函数一样,而不指定模块名称。这些函数调用将在编译时替换为完全限定的调用。

来自 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.

关于module - Erlang:包含模块和调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36091757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com