Function equality and ordering in Erlang

This behavior is different in shell and in compiled module. As for == and =:= operators in modules I am pretty sure that these operators return true iff:

  • Both functions are defined in the same place in code (isn't true in shell!)
  • Their effective closures are equal according to the respective (==/=:=) operator.

For instance with code

test(A, B) ->
  fun(C) -> {A, C} end.

the following shall hold:

> test(1, x) == test(1, y).
true.
> test(1, x) =:= test(1, y).
true.
> test(1, x) == test(1.0, y).
true.
> test(1, x) =:= test(1.0, y).
false.

Note that B does not belong to the closure of inner function therefore not influencing the comparison result.


The difference between the 2 evaluation in the shell comes from the blank line 6>. if you have a look at the fun using the function erlang:fun_info/1, you will see that in that case, the clause is stored with a different number (ie 2 instead of 1).

If you enter again the definition of Y (without a blank line) you will get a bad match, if you enter a blank line before, it is OK.

I think that this is a side effect of using the shell, but that the behavior is consistent within a program. Of course the meaning of > or < is not obvious for a fun, but == yes. A nice thing also is that the order of Erlang term is defined, so it is possible to sort a list of any term with predictable behavior:

number < atom < reference < fun < port < pid < tuple < list < bit string