一個常見的錯誤消息如下:二郎:在Erlang程序不匹配的右手邊值
** exception error: no match of right hand side value 'foo'
in function module:function/2 (file.erl, line 42)
如何調試呢?
一個常見的錯誤消息如下:二郎:在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
新變量
運行程序再次打印這個值變量,並明白爲什麼它不給定的模式
符合以下是一些例子:
Fresh1 = io:get_line("Do you want to chat?"),
erlang:display(Fresh1),
{_, Input} = Fresh1
再次運行程序:
{_, Input} = io:get_line("Do you want to chat?") % Line 42
替換爲這個
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殼:
2> Pid = echo:start().
** exception error: no match of right hand side value <0.41.0>
這裏變量的Pid絕對是已經綁定另一個值...
3> Pid.
<0.39.0>
可以使殼忘記了這樣一個綁定與f(Var)
或f()
:
4> f(Pid).
ok
5> Pid.
* 1: variable 'Pid' is unbound
6> Pid = echo:start().
<0.49.0>
錯誤報告更友好的簡單糟糕的比賽,這也給失敗匹配值。如果這個值是「小」的是,它完全顯示,它往往是不夠了解了什麼錯誤:
1> c(err).
{ok,err}
2> err:test().
an input ? ok
"ok\n"
3> err:test().
an input ? ko
** exception error: no match of right hand side value "ko\n"
in function err:test/0 (err.erl, line 6)
4> F = fun() -> a = 10 end.
#Fun<erl_eval.20.106461118>
5> F().
** exception error: no match of right hand side value 10
6> err:test2().
an input ? ok
"ok\n"
7> err:test2().
an input ? ko
** exception error: no match of right hand side value "ko\n"
in function err:'-test2/0-fun-0-'/0 (err.erl, line 10)
8>
:
-module(err).
-export([test/0]).
test() ->
"ok\n" = io:get_line("an input ? ").
test2() ->
F = fun() ->
"ok\n" = io:get_line("an input ? ")
end,
F().
在外殼