syntaxerlangfunction-callarity

Specifying method based on arity in Erlang


I am having an Erlang module that exports two methods with the same name but different arity: proc/1 and proc/2.

When using the methods in the form of MFA , how do you specify that the /2 should be used or /1? See for example:

spawn(?MODULE,proc,[22])  % how to tell i want the `/1` arity  method
spawn(?MODULE,proc,[11,22]) % `/2`arity method

Solution

  • The number of elements in your list of arguments specifies if you are using /1or /2:

    1> apply(lists, reverse, [[a, b, c]]).
    [c,b,a]
    2> apply(lists, reverse, [[a, b, c], [tail1, tail2]]).
    [c,b,a,tail1,tail2]
    3> length([[a, b, c]]).
    1
    4> length([[a, b, c], [tail1, tail2]]).
    2
    

    Here I am using apply/3 and using the Module:Function:Args format to first call reverse/1 and then reverse/2.