Erlang 中的 compile(export_all) 和 export([all/0]) 有什么区别?

What is the difference between compile(export_all) and export([all/0]) in Erlang?

似乎两者在使用时具有相同的效果,因为在编译代码时导出所有函数。两者之间有区别吗? export([all/0]). 是否导出所有函数而不需要编译?

Pouriya的回答我看了好几遍,直到第三遍才明白Pouriya的意思

Pouriya 想说的是 export([all/0]) 并没有按照您的想法行事。相反,它导出一个名为 all() 的函数,并且不会导出模块中的其他函数。这很容易测试:

-module(my).
-export([all/0]).

all() -> ok.
go() -> ok.

在shell中:

~/erlang_programs$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V9.3  (abort with ^G)
1> c(my).
my.erl:5: Warning: function go/0 is unused
{ok,my}

马上你会得到一个警告:因为 go() 没有被导出,这意味着你不能从模块外部调用它,并且因为模块内部没有函数调用 go(),函数 go() 是 "unused"。换句话说,go() 永远不会执行,所以编译器想知道为什么你首先定义了 go()

但是,假设您无法弄清楚该警告的含义(毕竟它只是一个警告):

2> my:all().
ok

3> my:go().
** exception error: undefined function my:go/0

Seems like the two has the same effect

没有:

-module(my).
-compile([export_all]).

all() -> ok.
go() -> ok.

在shell中:

/erlang_programs$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3  (abort with ^G)

1> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}

2> 

没有关于 go() 为 "unused" 的警告。并且:

2> my:all().
ok

3> my:go().
ok

4> 

调用 go() 时没有错误。