[erlang-questions] A suggested improvement for -extends(…)

Fernando "Brujo" Benavides <>
Fri Dec 23 15:17:28 CET 2011


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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111223/c4ae3644/attachment.html>


More information about the erlang-questions mailing list