[erlang-questions] How to avoid copying code?

Håkan Stenholm hokan.stenholm@REDACTED
Sat Nov 25 02:47:20 CET 2006


Håkan Stenholm wrote:

>attila.rajmund.nohl@REDACTED wrote:
>
>  
>
>>Hello!
>>
>>I have a couple of modules (20-30) which define the same macros, like
>>this:
>>module1.erl:
>>-define(FOO, "something1").
>>-define(BAR, "else1").
>>
>>module2.erl:
>>-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).
>>
>>				Bye,NAR
>> 
>>
>>    
>>
>One possible way to remove the need for having multiple get_data/0 
>functions would be to write something like:
>
>module1.erl:
>foo() -> "something1".
>bar() -> "else1".
>
>module2.erl:
>foo() -> "something2".
>bar() -> "else2".
>
>single_get_data_module.erl:
>get_data(Module) ->
>    {Module:foo(), Module:bar()}.
>
>
>You can then call single_get_data_module:get_data/1 with the appropriate 
>module, when you need a specific tuple.
>  
>
minor clarification, Module is a module name e.g.

single_get_data_module:get_data(module1) would return {"something1", "else1"}


>_______________________________________________
>erlang-questions mailing list
>erlang-questions@REDACTED
>http://www.erlang.org/mailman/listinfo/erlang-questions
>
>  
>



More information about the erlang-questions mailing list