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)
.
- What's the difference in Elixir from other (let's say imperative) programming languages in sense of distinction between variable and function?
- 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)
1条答案
按热度按时间2izufjch1#
Elixir过去认为括号对于所有函数调用都是可选的,包括
Kernel.node/0
这样的0元函数:此行为已被弃用,并将发出编译时警告:
非限定调用的括号是可选的,但零元调用除外,零元调用与变量一起使用时将不明确。
但是,由于仅仅更改此行为将是一个破坏性的更改,因此它仍然有效,并且在您的情况下被解释为
list()
。这可能会在Elixir 2.0中改变。关于此主题的讨论here。