[erlang-questions] parameterized modules and OO

Ulf Wiger (TN/EAB) <>
Wed May 30 15:43:40 CEST 2007


Erlhive makes extensive use of parameterized modules
in order to carry user context.

I modify the module variables sometimes, but do so
through a code rewrite, and then explicitly call
the function with new module parameters

...making use of the fact that the actual call
to a parameterized module looks like:

{Mod,Params|...}:F(...)

Example:

-module(pmod, [X, Y]).

-export([set_Y/1,
	 sum/0]).


sum() ->
    X + Y.

set_Y(NewY) ->
    {?MODULE, X,NewY}.

Eshell V5.5.4.3  (abort with ^G)
1> 
1> c(pmod).
{ok,pmod}
2> M = pmod:new(3,4).
{pmod,3,4}
3> M:sum().
7
4> M1 = M:set_Y(5).
{pmod,3,5}
5> M1:sum().
8


BR,
Ulf W

> -----Original Message-----
> From:  
> [mailto:] On Behalf Of 
> Torbjorn Tornkvist
> Sent: den 30 maj 2007 14:10
> To: 
> Subject: [erlang-questions] parameterized modules and OO
> 
> Hi all,
> 
> I just read the very cool paper about "parameterized modules"
> and cooked up a little experimental OO framework. This makes 
> it possible to write code, such as:
> 
>   M = mstring:new("hello").
>   M:len(). => 5
>   M:append(" world"). => "hello world"
>   M:len() => 11
> 
> This code is using an ETS table to store the state, i.e boring...
> (code can be found here: http://www.tornkvist.org/gitweb look 
> for the mobject project)
> 
> So I was thinking, would it be possible for a parameterized 
> module to modify its own module attributes ?
> 
> For example:
> 
> ...
> -store([]).
> 
> append(Str) ->
>     module_info(store, module_info(store) ++ Str).
> 
> This way, the ETS table could go away and we have a 
> self-contained object. One should even be able to add a 
> simple inheritance mechanism.
> 
> So would this be possible to add without too much hassle ?
> 
> Cheers, Tobbe
> (NB: I'm not saying this is good or bad, I'm not even 
> proposing this, I'm just curious to hear if it would be 
> easy/hard/impossible to add this functionality.)
> 
> 
> 
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 




More information about the erlang-questions mailing list