[erlang-questions] How to avoid copying code?

Mazen <>
Sat Nov 25 11:53:47 CET 2006

Guest wrote:

I have a couple of modules (20-30) which define the same macros, like
-define(FOO, "something1").
-define(BAR, "else1").

-define(FOO, "something2").
-define(BAR, "similar2").

And I also have a function in these modules that return the data of
these macros:

get_data() ->
 	{?FOO, ?BAR}.

Obviously this function definition is exactly the same in all 20-30
modules, which I don't really like. Is there a way to define this
function only once? In Java I'd create an abstract ancestor class for
module1 and module2, declare the FOO and BAR variables there, also
implement the get_data() in the ancestor class and assign values to FOO
and BAR in the constructor of module1 and module2 (there is only one
instance of module1, etc. classes in the program).

"Beware of bugs in the above code; I have only proved it correct, not
  tried it."
erlang-questions mailing list

 Post recived from mailinglist
(end of quote)

If i understand you correctly (you having programmed Java and all), you see get_data() as an accessor function (method?) to the "object's" (this case being the module's) data. This is clearly a very object oriented way of seeing things. So first my personal answer would be: You should probably try to change your design according to the tools you have (in this case Erlang). Secondly im curious: Why  would you want to have accessor functions to data that is static?
Post sent from http://www.trapexit.org

More information about the erlang-questions mailing list