【发布时间】:2017-12-15 18:23:21
【问题描述】:
我遇到了一些我没想到的行为……这正常吗?
Interactive Elixir (1.5.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> test = %{a: 0}
%{a: 0}
iex(2)> test.a
0
iex(3)> test.a - 1
-1
iex(4)> test.a -1
** (ArgumentError) argument error
:erlang.apply(%{a: 0}, :a, [-1])
嗯,解决方法很简单。我只是想明白,为什么会这样。
【问题讨论】:
-
是的,这是正常的。在这种情况下,它被解析为
test.a(-1),因此会产生一个错误。 -
有趣的是,当你在一个简单的绑定变量
a上尝试它时,你会得到一个更明确的错误:** (CompileError) iex:3: "a -1" looks like a function call but there is a variable named "a", please use explicit parentheses or even spaces -
然而,当我尝试
1 -1时,它并没有像函数调用那样被解析...... :) 猜猜这意味着没有函数只能用数字命名
标签: elixir