erlang 为什么有一个函数未定义的消息,而它是关于变量

whhtz7ly  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(182)

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)
2izufjch

2izufjch1#

Elixir过去认为括号对于所有函数调用都是可选的,包括Kernel.node/0这样的0元函数:

iex> node
:nonode@nohost

此行为已被弃用,并将发出编译时警告:

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

非限定调用的括号是可选的,但零元调用除外,零元调用与变量一起使用时将不明确。
但是,由于仅仅更改此行为将是一个破坏性的更改,因此它仍然有效,并且在您的情况下被解释为list()
这可能会在Elixir 2.0中改变。关于此主题的讨论here

相关问题