Chen Yu
Chen Yu

Reputation: 4077

What's the meaning of the following statement from file gproc_lib.erl?

When reading github's Project gproc's source code file "gproc_lib.erl", I have faced some problem. Where could I find some related reference doc for this statement syntax?

check_option_f(ets_options)    -> fun check_ets_option/1; **%<----**What's the meaning of this** statement**?
check_option_f(server_options) -> fun check_server_option/1.

check_ets_option({read_concurrency , B}) -> is_boolean(B);
check_ets_option({write_concurrency, B}) -> is_boolean(B);
check_ets_option(_) -> false.

check_server_option({priority, P}) ->
    %% Forbid setting priority to 'low' since that would
    %% surely cause problems. Unsure about 'max'...
    lists:member(P, [normal, high, max]);
check_server_option(_) ->
    %% assume it's a valid spawn option
    true.

Upvotes: 1

Views: 96

Answers (1)

aronisstav
aronisstav

Reputation: 7914

fun module:name/arity is a function value, equivalent to the following:

fun(A1,A2,...,AN) -> module:name(A1,A2,...,AN) end

where N is the arity. In short it is a useful shorthand to pass normal Erlang functions as arguments to other functions which expect a function as an argument.

Example:

To convert a list List to a set:

lists:foldl(fun sets:add_element/2, sets:new(), List).

Equivalent to:

lists:foldl(fun (E, S) -> sets:add_element(E, S) end, sets:new(), L).

(The latter is the definition used in OTP's set module for the from_list function.)

More info here.

Upvotes: 5

Related Questions