Erlang:没有右手边值的匹配 [英] Erlang: no match of right hand side value

查看:262
本文介绍了Erlang:没有右手边值的匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Erlang程序中的常见错误消息如下:

A common error message in Erlang programs is the following:

** exception error: no match of right hand side value 'foo'
     in function module:function/2 (file.erl, line 42)

如何调试这个?

推荐答案

这是如何调试这种错误:

Here is how to debug this kind of error:


  1. 转到 module:function / 2(file.erl,第42行)

找到绝对有争议的匹配操作

Find the offending matching operation that is definitely there

将左侧替换为新鲜的变量。在这里,您可能会发现您正在尝试对已经绑定的变量进行模式匹配...

Replace the left-hand side with a fresh variable. Here you may figure out that you are trying to pattern-match against an already bound variable...

添加一个对 erlang:display / 1 with新变量

Add a call to erlang:display/1 with the new variable

再次运行该程序以打印此变量的值,并了解为什么它与给定模式不匹配

Run the program again to print the value of this variable and understand why it doesn't match the given pattern

以下是一些示例:

{_, 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} 的匹配失败。

You can see that io:get_line/1 returns a string and not a tuple, so the matching against {_, Input} fails.

示例2

在Erlang shell中:

In an Erlang shell:

2> Pid = echo:start().
** exception error: no match of right hand side value <0.41.0>

这里变量Pid绝对是已经绑定到另一个值...

Here the variable Pid is definitely already bound to another value...

3> Pid.
<0.39.0>

你可以让shell忘记这样一个绑定,使用 f(Var) / code>或 f()

You can make the shell forget such a binding with f(Var) or f():

4> f(Pid).
ok
5> Pid.
* 1: variable 'Pid' is unbound
6> Pid = echo:start().
<0.49.0>


这篇关于Erlang:没有右手边值的匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆