Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the slash notation in Elixir mean?

Tags:

elixir

In the Elixir docs, they keep using an odd notation with slash, for example:

is_boolean/1
IO.puts/1
String.length/1
is_function/2
++/2

I'm just guessing, but I think it refers to arity. But if that's the case, why the devil isn't it mentioned anywhere in the docs? It's not as if this is any kind of standard convention in IT (at least, not one that I've ever seen in my 20+ years in IT).

like image 952
Richard Eng Avatar asked Apr 21 '16 18:04

Richard Eng


2 Answers

From page 2, Basic types of the Getting started documentation:

Note: Functions in Elixir are identified by name and by number of arguments (i.e. arity). Therefore, is_boolean/1 identifies a function named is_boolean that takes 1 argument. is_boolean/2 identifies a different (nonexistent) function with the same name but different arity.

It is also described in Erlang/Elixir Syntax: A Crash Course:

Here we create a module named hello_module. In it we define three functions, the first two are made available for other modules to call via the export directive at the top. It contains a list of functions, each of which is written in the format <function name>/<arity>. Arity stands for the number of arguments.

I might speculate that this tends to be relegated to a side note in Elixir literature because it comes straight from Erlang. Although Erlang knowledge shouldn't be necessary to use Elixir, such omissions are a common mistake when people document software that is derived as Elixir is from Erlang.

like image 118
Chris Martin Avatar answered Oct 15 '22 12:10

Chris Martin


You guessed right that it's the arity of the function. The reason why it's an important information (that is often not included in many languages) is that functions with the same name, but different arity are different functions - an example of that is Enum.reduce/2 and Enum.reduce/3. A function in Elixir is identified by three things: module, name and arity. If any single one of these is different, then you have a different function.

The notation is also mentioned in the Getting Started guide: 1, 2.

like image 36
michalmuskala Avatar answered Oct 15 '22 10:10

michalmuskala