Erlang 程序中的一个常见错误消息如下:
** exception error: no match of right hand side value 'foo'
in function module:function/2 (file.erl, line 42)
我该如何调试呢?
最佳答案
以下是如何调试此类错误:
module:function/2 (file.erl, line 42)
erlang:display/1
使用新变量 这里有些例子:
{_, Input} = io:get_line("Do you want to chat?") % Line 42
将此替换为:
Fresh1 = io:get_line("Do you want to chat?"),
erlang:display(Fresh1),
{_, Input} = Fresh1
再次运行程序:
1> module:run().
Do you want to chat? Yes
"Yes\n"
** exception error: no match of right hand side value "Yes\n"
in function module:function/2 (file.erl, line 44)
你可以看到
io:get_line/1
返回一个字符串而不是一个元组,所以匹配 {_, Input}
失败。 在 Erlang shell 中:
2> Pid = echo:start().
** exception error: no match of right hand side value <0.41.0>
这里的变量 Pid 肯定是 已绑定(bind)到另一个值...
3> Pid.
<0.39.0>
您可以使用
f(Var)
让 shell 忘记这样的绑定(bind)。或 f()
:4> f(Pid).
ok
5> Pid.
* 1: variable 'Pid' is unbound
6> Pid = echo:start().
<0.49.0>
关于Erlang:右手边值不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23577935/