Quantcast
Channel: Why are there two kinds of functions in Elixir? - Stack Overflow
Viewing all articles
Browse latest Browse all 10

Why are there two kinds of functions in Elixir?

$
0
0

I'm learning Elixir and wonder why it has two types of function definitions:

  • functions defined in a module with def, called using myfunction(param1, param2)
  • anonymous functions defined with fn, called using myfn.(param1, param2)

Only the second kind of function seems to be a first-class object and can be passed as a parameter to other functions. A function defined in a module needs to be wrapped in a fn. There's some syntactic sugar which looks like otherfunction(&myfunction(&1, &2)) in order to make that easy, but why is it necessary in the first place? Why can't we just do otherfunction(myfunction))? Is it only to allow calling module functions without parenthesis like in Ruby? It seems to have inherited this characteristic from Erlang which also has module functions and funs, so does it actually comes from how the Erlang VM works internally?

It there any benefit having two types of functions and converting from one type to another in order to pass them to other functions? Is there a benefit having two different notations to call functions?


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images