[erlang-questions] basic import for code reuse

Adam Lindberg <>
Thu Dec 6 14:15:55 CET 2007


Yes there is a technical reason, if you call the function member/0 in your
module, how should the compiler know which module you intented to call? The
imported one or the one declared in your module?

Overriding is not possible in Erlang at the moment. Although there was a
presentation on the subject (and others) and the Erlang User Conference
(EUC) this year: http://www.erlang.se/euc/07/papers/1700Carlsson.pdf

One simple way to "solve" your problem though is to just not import the
member/0 function in the module you want to override it in. This way both
module will have the member/0 function anyway. Thus you have to tell the
modules that are not to override it to import the template function instead.


Cheers,
Adam

On Dec 6, 2007 6:49 AM, Robin <> wrote:

> lw_template.erl:
> -module(lw_template).
> -compile(export_all).
> member() -> template.
>
> lw_special.erl:
> -module(lw_special).
> -compile(export_all).
> -import(lw_template, [member/0]).
> member() -> special.
>
> The error output:
> Eshell V5.5.5  (abort with ^G)
> 1> lw_special:member().
> ** exited: {undef,[{lw_special,member,[]},
>                   {erl_eval,do_apply,5},
>                   {shell,exprs,6},
>                   {shell,eval_loop,3}]} **
>
> Is there any technical reason that you cannot override an imported
> function?
>
> Thanks,
>
> Robin
>
>
> On Dec 5, 7:41 pm, Robin <> wrote:
> > This is a simple example using -import:
> >
> > -module(lw_template).
> > -compile(export_all).
> > member() -> template.
> >
> > -module(lw_special).
> > -compile(export_all).
> > -import(lw_template, [member/0]).
> > member() -> special.
> >
> > When I import member/0 and then override it, the compiler gives the
> > warning:
> >
> > lw_special.erl: defining imported function member/0
> >
> > When I call member/0, the runtime bombs with an error:
> >
> > Eshell V5.5.5  (abort with ^G)
> > 1> ls_special:member().
> > ** exited: {undef,[{ls_special,member,[]},
> >                    {erl_eval,do_apply,5},
> >                    {shell,exprs,6},
> >                    {shell,eval_loop,3}]} **
> >
> > Import followed by override does not work above, so how else can you
> > specialize an imported function at compile time?
> >
> > If the runtime bombs from overriding a function, then should the
> > compiler produce an error rather than just a warning?
> >
> > thanks,
> >
> > Robin
> >
> > _______________________________________________
> > erlang-questions mailing list
> > 
> .orghttp://www.erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20071206/baf0b1d5/attachment.html>


More information about the erlang-questions mailing list