Reputation: 494
If I declare a function
test(A) -> 3.
Erlang generates a warning about variable A
not being used. However the definition
isEqual(X,X) -> 1.
Doesn't produce any warning but
isEqual(X,X) -> 1;
isEqual(X,Y) -> 0.
again produces a warning but only for the second line.
Upvotes: 1
Views: 175
Reputation: 9930
The reason why that doesn't generate a warning is because in the second case you are asserting (through pattern matching), by using the same variable name, that the first and second arguments to isEqual/2
have the same value. So you are actually using the value of the argument.
It might help to understand better if we look at the Core Erlang code produced from is_equal/2
. You can get .core
source files by compiling your .erl
file in the following way: erlc +to_core pattern.erl
(see here for pattern.erl
).
This will produce a pattern.core
file that will look something like this (module_info/[0,1]
functions removed):
module 'pattern' ['is_equal'/2]
attributes []
'is_equal'/2 = fun (_cor1,_cor0) ->
case <_cor1,_cor0> of
%% Line 5
<X,_cor4> when call 'erlang':'=:=' (_cor4, X) ->
1
%% Line 6
<X,Y> when 'true' ->
0
end
As you can see, each function clause from is_equal/2
in the .erl
source code gets translated to a case
clause in Core Erlang. X
does get used in the first clause since it needs to be compared to the other argument. On the other hand neither X
or Y
are used in the second clause.
Upvotes: 6