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

Igor Ribeiro Sucupira <>
Wed Jun 9 07:42:58 CEST 2010


On Tue, Jun 8, 2010 at 10:53 PM, Richard O'Keefe <> wrote:
>
> The mistake here isn't that bar/1 isn't used, but that
> it's not *exported*.  We can see this even more clearly
> if we change the example:
>
>     1  -module(foo).
>     2  -export([bah/1]).
>     3  bar(X) when X < 0 -> ugh(-X);
>     4  bar(X) -> X+1.
>     5  ugh(X) when X < 0 -> bar(-X);
>     6  ugh(X) -> X-1.
>
>     ./foo.erl:2: function bah/1 undefined
>     ./foo.erl:3: Warning: function bar/1 is unused
>     ./foo.erl:5: Warning: function ugh/1 is unused
>
> This time there *is* a local function call to bar/1
> and there *is* a local function call to ugh/1.  They
> are both *used*.  What they aren't is *reachable*.
>
> Now let's change the example one more time.
>
>     1  -module(foo).
>     2  -export([zoo/0]).
>     3  bar(X) when X < 0 -> ugh(-X);
>     4  bar(X) -> X+1.
>     5  ugh(X) when X < 0 -> bar(-X);
>     6  ugh(X) -> X-1.
>     7  zoo() -> is_function(fun bar/1), is_function(fun ugh/1).
>
>     ./foo.erl:7: Warning: the call to is_function/1 has no effect
>
> The functions bar/1 and ugh/1 are no more reachable than before.
> There is no possible call from the outside that will ever result in
> either function being invoked.  But suddenly they are "used".

What I see here is that the function bar/1 will never be executed and
will never be called (consider code execution), but its value is used
in code that can be executed and that's something that I call "usage".
For example: if you remove the function, reachable code breaks.
So maybe this is one of the main sources of disagreement in this
thread: the strength one thinks there is in the relation between
"reachable code" and the concept of "usage".

Best regards.
Igor.


More information about the erlang-questions mailing list