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

Richard A. O'Keefe ok@REDACTED
Fri Sep 30 03:54:06 CEST 2016


On 30/09/16 3:22 AM, Donald Steven wrote:
[The basic problem is that
    % Tag is defined here, Datum and Value are not
    receive {Tag,Datum} -> Value = f(Datum) end,
    use(Datum, Value)
  produces warnings about unsafe variables.]

If a variable is defined in some branch(es) of a case or receive
but not others, it is in scope after the case or receive, but as
it might not be defined, it is unsafe.

In this case, it looks to me as if the compiler is wrong.
There is no path through the receive that doesn't define all
the variables.

You could write

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

which the compiler is happy with.



More information about the erlang-questions mailing list