Does anyone have any idea of what is going wrong?
Here is an example...Erlang defines a function error/2
, known as a BIF or Built In Function, which can be called like this:
-module(a).
-compile(export_all).
go(X, Y) ->
case X > 10 of
true -> error(bad_arg, [X, Y]);
false -> ok
end,
io:format("hello\n").
In the shell:
4> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
5> a:go(1, 2).
hello
ok
6> a:go(11, 2).
** exception error: bad_arg
in function a:go/2
called as a:go(11,2)
7>
Now, look what happens if you define a function also named error/2
in your module:
-module(a).
-compile(export_all).
error(X, Y) ->
io:format("X = ~w, Y = ~w~n", [X,Y]).
go(X, Y) ->
case X > 10 of
true -> error(bad_arg, [X, Y]);
false -> ok
end,
io:format("hello\n").
In the shell:
8> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
a.erl:9: Warning: ambiguous call of overridden auto-imported BIF error/2
- use erlang:error/2 or "-compile({no_auto_import,[error/2]})." to resolve name clash
{ok,a}
The warning tells you that if you really meant to call erlang's error/2
, then you should preceed the function name with the module name in which the function is defined, namely the erlang
module:
case X > 10 of
true -> erlang:error(bad_arg, [X, Y]);
...or, if you want to call your version of error/2
, then put the module directive:
-compile({no_auto_import,[error/2]}).
at the top of your module:
-module(a).
-compile(export_all).
-compile({no_auto_import,[error/2]}).
error(X, Y) ->
io:format("X = ~w, Y = ~w~n", [X,Y]).
go(X, Y) ->
case X > 10 of
true -> error(bad_arg, [X, Y]);
false -> ok
end,
io:format("hello\n").
11> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
Note that the problem in the code is only a warning
, and when I run my program as originally written, erlang calls the version of error/2
defined in my module--not the one in the erlang
module.
The strange thing is: there is no error/3
defined in the erlang module.