Erlang: no matching right side value

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?

+1
source share
2 answers

Here's how to debug such an error:

  • Switch to module:function/2 (file.erl, line 42)

  • Find the reconciliation operation that definitely exists

  • Replace the left side of the variable fresh . Here you can understand that you are trying to map a pattern to an already associated variable ...

  • Add a call erlang:display/1with a new variable

  • Run the program again to print the value of this variable and understand why it does not match the given pattern.

Here are some examples:

  • Example 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} .

  • 2:

    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>
    
+5

, , , . "", , , :

-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().

:

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>  
+1

Source: https://habr.com/ru/post/1530825/


All Articles