Erlang: no match of right hand side value

erlang, pattern-matching

Solution

Here is how to debug this kind of error:

Go to `module:function/2 (file.erl, line 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...

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

Here are some examples:

Example 1:

{_, Input} = io:get_line("Do you want to chat?") % Line 42

Replace this with:

Fresh1 = io:get_line("Do you want to chat?"),
erlang:display(Fresh1),
{_, Input} = Fresh1

Run the program again:

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)

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

Example 2:

In an Erlang shell:

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

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

3> Pid.
<0.39.0>

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>

Problem

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) ``` How can I debug this?

Original source

Related problems