pydem
pydem

Reputation: 81

Why is there a function undefined message whereas it's about variable

Let's say I'm invoking something like this

Enum.count(list)

And list is not defined in 'upper scope'. In most of languages you'll probably get something like Variable list is undefined, but in Elixir (it comes from Erlang, so I hope it's same behaviour) you'll be getting undefined function list/0 (there is no such import).

  1. What's the difference in Elixir from other (let's say imperative) programming languages in sense of distinction between variable and function?

  2. Also I've noticed you can make a function in module, and if it takes zero arguments, you can call it without parentheses, I was wondering what's special about that. (was answered below by @sabiwara)

Upvotes: 2

Views: 168

Answers (1)

sabiwara
sabiwara

Reputation: 3134

Elixir used to consider parentheses optional for all function calls, including 0-arity functions like Kernel.node/0:

iex> node
:nonode@nohost

This behavior has since been deprecated and will emit a compile time warning:

warning: variable "node" does not exist and is being expanded to "node()", please use parentheses to remove the ambiguity or change the variable name

Parentheses for non-qualified calls are optional, except for zero-arity calls, which would then be ambiguous with variables.

But since it would be a breaking change to just change this behavior, it still works and gets interpreted, in your case, as list().

This might change in Elixir 2.0. A discussion on this topic here.

Upvotes: 2

Related Questions