Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this variable unused?

Tags:

erlang

Why does compiling this code:

triples( [], _,_,_)->
  [];

triples( Self, X, Y, none )->
  [ Result || Result = { X, Y, _} <- Self ].

report:

./simple_graph.erl:63: Warning: variable 'X' is unused
./simple_graph.erl:63: Warning: variable 'Y' is unused
./simple_graph.erl:64: Warning: variable 'X' is unused
./simple_graph.erl:64: Warning: variable 'X' shadowed in generate
./simple_graph.erl:64: Warning: variable 'Y' is unused
./simple_graph.erl:64: Warning: variable 'Y' shadowed in generate

And return wrong result: full Self.

like image 644
r4ge Avatar asked Aug 14 '09 13:08

r4ge


1 Answers

This is because variables occurring on the LHS of generators, X and Y here, are always new unbound variables local to the comprehension. This means that they are not the same variables as the X and Y in the head of triples and, therefore, there is no implicit equality test. This similar to funs where all variables occurring in the head of a fun are alse new variables local to the fun.

This is different from most of the rest of erlang, which is why the compiler not only warns that the X and Y in the head are not used but also that the X and Y in the comprehension shadow the other variables. They are also unused anywhere in the comprehension.

An easy way to get what you want is:

[ Result || Result = {X1,Y1,_} <- Self, X =:= X1, Y =:= Y1 ]
like image 184
rvirding Avatar answered Oct 31 '22 19:10

rvirding