Erlang:如何在模块外部使用-define宏?

Enr*_*ico 6 erlang

假设我有模块test.erl,里面是宏TOTAL:

-module(test)
-export([...])

-define(TOTAL,(100))

...
Run Code Online (Sandbox Code Playgroud)

如果get_total()在test.erl中定义,我可以test:get_total().从REPL 调用

如何在?TOTAL不定义函数的情况下调用模块test.erl之外的(宏)?

Gre*_*ill 16

您可以将其-define放入test.hrl文件中,并将-include其包含在其他模块中.有关更多信息,请参阅Erlang预处理器文档.

test.hrl

-define(TOTAL, (100)).
Run Code Online (Sandbox Code Playgroud)

test.erl

-module(test).
-export([...]).

-include("test.hrl").

...
Run Code Online (Sandbox Code Playgroud)

other.erl

-module(other).

-include("test.hrl").

io:format("TOTAL=~p~n", [?TOTAL]).
Run Code Online (Sandbox Code Playgroud)