[erlang-questions] Overriding built-in functions in a module

Raimo Niskanen <>
Tue Jun 15 10:20:21 CEST 2010


On Tue, Jun 15, 2010 at 01:12:04PM +1200, Richard O'Keefe wrote:
> 
> On Jun 12, 2010, at 6:31 AM, Mikael Pettersson wrote:
> 
> >Richard O'Keefe writes:
> >>Why?  Suppose we have a module
> >>
> >>	-module(foo).
> >>	-export([bar/1]).
> >>	bar(N) when N > 0 -> ?MODULE:bar(N-1);
> >>	bar(0) -> 'DONG!'.
> >>
> >>In what sense is the call to ?MODULE:bar/1 a call to some *other*
> >>function than the bar/1 here before us?
> >
> >Because in Erlang a call with both module and function names
> >supplied is a "remote" call, which always invokes the latest
> >version of the module whose name was given.
> 
> No, the fact that a call is a remote call does NOT make it a
> call to a function OTHER THAN THE FUNCTION IT CALLS.
> 
> In this example, the function that is called is foo:bar/1.

My original point, that I tried to point out with the phrasing
"some other function", is that when the compiler sees a call to
foo:bar/1, within the module foo and there is no exported
function bar/1 in that module; it can not assume that
the call to foo:bar/1 is in error nor that it is supposed
to be a local call. It will become clear at runtime.

It is of course probable that either the call should be to bar/1 or
that bar/1 should be exported. But that is still speculation. Perhaps
the call should be to an exported function foo:bor/1 or foe:bar/1.

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list