[erlang-questions] A suggested improvement for -extends(…)
Fernando "Brujo" Benavides
fernando.benavides@REDACTED
Fri Dec 23 16:22:24 CET 2011
Ha! Super tricky, but I like your solution and it is similar to the way I would implement -extends: I would include all non-overridden parent functions in child.erl at compile time, but I get your point about code-reloading, I haven't thought about that.
Thanks!
On 23/12/2011, at 12:11, Fred Hebert wrote:
> The inheritance you see, is, as far as I know, just about redirecting calls. Let's say I have 2 modules, parent and child, where child extends parent.
>
> What happens is that when a call to child:some_function() fails because 'some_function/0' doesn't exist in child, the function is directly called as 'parent:some_function()' instead. Because of this, the macro cannot respect inheritance. It's simply a hack. The macro is a compile-time item while the -extends dispatching is a run-time mechanism.
>
> The feature you want would also have complex implications when it comes to code reloading: what if I change 'parent' and recompile it, reload it, without changing the child module?
>
> In general, I would advise against using -extends except when you know you really need it (i.e.: reimplementing a new AccessMod for Mnesia, where you have dozens and dozens of callbacks and you might want to only change one of them, while keeping the rest intact).
>
> What you want to do could instead be accomplished with a .hrl file. Just save
>
> -export([run/0, module/0, a_value/0]).
>
> run() ->
> io:format("Module: ~p~nA Value: ~p~n", [module(), a_value()]).
>
> module() -> ?MODULE.
>
> a_value() -> parent_value
>
> And then include that .hrl file in any module you want to have this (-include("my_file.hrl").). The ?MODULE macro will be supported fine in that place, and you will avoid all the issues of having local vs. remote calls. This is much easier and a lot less surprising for people using your code.
>
>
> On Fri, Dec 23, 2011 at 9:17 AM, Fernando "Brujo" Benavides <fernando.benavides@REDACTED> wrote:
> Hi fellows,
>
> Today I found myself writing a couple of modules with shared behavior (and what I mean by that is that except for an all/0 function with a different implementation in each module, the rest of their code is copy-pasted in each one of them). So, I said to myself: "This is a great place to use -extends!"
>
> And then I found myself in the following situation:
>
> --------------- parent.erl ---------------
> -module(parent).
>
> -export([run/0, module/0, a_value/0]).
>
> run() ->
> io:format("Module: ~p~nA Value: ~p~n", [module(), a_value()]).
>
> module() -> ?MODULE.
>
> a_value() -> parent_value
> ------------------------------------------
>
> ----------------- child.erl --------------
> -module(child).
>
> -extends(parent).
>
> -export([module/0, a_value/0]).
>
> module() -> ?MODULE.
>
> a_value() -> child_value.
> ------------------------------------------
>
> ----------------- console ----------------
> 2> parent:run().
> Module: parent
> A Value: parent_value
> ok
> 3> child:run().
> Module: parent
> A Value: parent_value
> ok
> ------------------------------------------
>
> It would've been a lot nicer to see different results, as follows...
>
> ----------------- console ----------------
> 2> parent:run().
> Module: parent
> A Value: parent_value
> ok
> 3> child:run().
> Module: child
> A Value: child_value
> ok
> ------------------------------------------
>
> In other words, I would like the ?MODULE macro to somehow respect inheritance (I know it may be difficult but I don't think it's impossible, right?) and/or functions to have a more OOP-like behavior, where if called from child module any overridden function that's called is used in its overridden version.
>
> Just a suggestion :)
>
> Cheers!
> ____________________________
> Fernando "Brujo" Benavides
> fernando.benavides@REDACTED
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
____________________________
Fernando "Brujo" Benavides
fernando.benavides@REDACTED
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111223/7e481260/attachment.htm>
More information about the erlang-questions
mailing list