Errlang: no match of right hand side value

2020-04-08 01:23发布

问题:

I'm getting started with Erlang, but I'm already in troubles. I copied this example from a book:

-module(echo).
-export([start/0, loop/0]).

start() ->
    spawn(echo, loop, []).

loop() ->
    receive
        {From, Message} ->
            From ! Message,
            loop()
    end.

But when I try it I get an error I don't understand:

31> c(echo).          
{ok,echo}
32> f.      
f
33> Pid = echo:start().
** exception error: no match of right hand side value <0.119.0>

Why does this happen?

回答1:

Probably, 'Pid' has some value assigned already and you're trying to re-assign it.

Here is how it behaves on my machine:

Eshell V5.9.1  (abort with ^G)
1> c(echo).
{ok,echo}
2> f.
f
3> Pid = echo:start().
<0.39.0>
4> Pid = echo:start().
** exception error: no match of right hand side value <0.41.0>
5>

As you can see, the first 'Pid = ' construction woks fine, but the second one throws error message you described.

I think, you used Pid in the shell before already and it has some value assigned.

Try to 'reset' Pid variable and use it like following:

8> f(Pid).
ok
9> Pid.
* 1: variable 'Pid' is unbound
10> Pid = echo:start().
<0.49.0>

Or you can forget all variables by using such a construction:

13> f().
ok
14> Pid = echo:start().
<0.54.0>

Pay attention on used f(). - not just f.



标签: erlang