Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Erlang's export syntax /x mean? Why the slash and then a number?

Tags:

erlang

-export([consult/1,
         dump/2, 
         first/1, 
         for/3,
         is_prefix/2).

I'm reading documentation and confused with syntax above. What's the meaning of /1, /2, /3 in the list above?

like image 795
DmitrySemenov Avatar asked Nov 28 '12 03:11

DmitrySemenov


2 Answers

/1, /2, /3 etc are referred to as the "Arity" of the function, Arity meaning the number of arguments accepted by that function.

In Erlang, two functions of with the same name but with different arity are two different functions, and as such are each exported explicitly. To quote the Erlang documentation is says:

A function is uniquely defined by the module name, function name, and arity.

For example, if you have two functions:

do_something() -> does_something().

do_something(SomeArg) -> some_something_else(SomeArg).

And at the top of your module, you had only

-export([do_something/0]).

Then only the do_something with zero arguments would be exported (that is, accessible from other modules in the system).

like image 195
chops Avatar answered Oct 26 '22 19:10

chops


It is the function signature.

consult/1 means the function named consult accepts an argument. dump/2 means the function dump accepts two arguments.

Consult the documentation for more info

like image 33
kimhyunkang Avatar answered Oct 26 '22 20:10

kimhyunkang