假设我有模块 test.erl,里面是宏 TOTAL
:
-module(test)
-export([...])
-define(TOTAL,(100))
...
如果
get_total()
在 test.erl 中定义,我可以调用 test:get_total().
来自 REPL如何调用
?TOTAL
(宏)在模块 test.erl 之外没有定义函数?
最佳答案
你可以把 -define
在 test.hrl
文件,并使用 -include
将其包含在其他模块中。见Erlang Preprocessor documentation了解更多信息。
测试.hrl
-define(TOTAL, (100)).
测试.erl
-module(test).
-export([...]).
-include("test.hrl").
...
其他.erl
-module(other).
-include("test.hrl").
io:format("TOTAL=~p~n", [?TOTAL]).
关于Erlang:如何在模块外部使用 -define 宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3816863/