What does the slash notation in Elixir mean?

岁酱吖の 提交于 2020-01-03 07:05:12

问题


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).


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/36778114/what-does-the-slash-notation-in-elixir-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!