[erlang-questions] Newbie help please with variable unsafe in 'receive' & variable unused

Vlad Dumitrescu vladdu55@REDACTED
Thu Sep 29 17:02:58 CEST 2016


The crash is because the message doesn't look like I assumed. I didn't pay
attention and didn't see that it should be {{X, Y, Z}} = Coordinates (two
levels of braces)

To get rid of the warning, you can rewrite

loop(Orbiter1) ->
    receive
        {Orbiter1, Coordinates={{X,Y,Z}}} ->
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z])
    end,
    loop(Orbiter1).

For a real server, you probably want to handle more messages, and ignore
those you don't care about. For example

loop(Orbiter1) ->
    receive
        {Orbiter1, Coordinates={{X,Y,Z}}} ->
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z]),
            loop(Orbiter1);
        _ ->
            ok % stop if unknown message arrives
    end.

regards,
Vlad


On Thu, Sep 29, 2016 at 4:52 PM, Donald Steven <t6sn7gt@REDACTED> wrote:

> Alas that produces:
>
> problem.erl:26: Warning: variable 'Coordinates' exported from 'receive'
> (line 26)
> {"init terminating in do_boot",{{badmatch,{{-4.99800
> 0e+02,6.000000e-01,2.000000e-01}}},[{problem,loop,1,[{file,
> "problem.erl"},{line,29}]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
> init terminating in do_boot ()
>
> On 09/29/2016 10:34 AM, Vlad Dumitrescu wrote:
>
> loop(Orbiter1) ->
>     Coordinates = receive
>         {Orbiter1, Coordinates} -> Coordinates
>     end,
>     {X, Y, Z} = Coordinates,
>     io:format("Coordinates: ~p~n", [Coordinates]),
>     io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z])
>     loop(Orbiter1).
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160929/95eb3851/attachment.htm>


More information about the erlang-questions mailing list