Erlang - Anonymouos功能

laa*_*hin 1 erlang anonymous-function

如果我调用test(),它就不起作用.有人可以解释一下吗?

-module(anony).

-export([test/0, test1/0]).

test1() -> "hello".

test() ->
   C = fun(F) -> Val = F(), io:format("~p ", [Val]) end, 
   lists:foreach(debug, [test1]).
Run Code Online (Sandbox Code Playgroud)

cth*_*ops 7

test1它本身就是一个原子,而不是对本地函数的引用.要创建对该函数的引用,请使用fun fun/Arity,如下所示.

-module(anony).

-export([test/0, test1/0]).

test1() -> "hello".

test() ->
    C = fun(F) -> Val = F(), io:format("~p ", [Val]) end,
    lists:foreach(C, [fun test1/0]).
Run Code Online (Sandbox Code Playgroud)

你也可以构造一个test1像这样调用的匿名函数: fun() -> test1() end但除非你想要传入其他值,否则没有理由这样做.