[erlang-questions] Parameterized module initialization

OvermindDL1 overminddl1@REDACTED
Thu Jun 28 03:23:06 CEST 2012


On Tue, Jun 26, 2012 at 6:53 AM, Joe Armstrong <erlang@REDACTED> wrote:

> There is an undocumented way of doing this (which one day will become
> standard)
>
> Suppose we define X as follows:
>
> > X = {mod1, a,b,c}.
> {mod1, a,b,c}.
>
> X is now an instance of a parameterised module. if we now call the
> function X:func1(x,y) then what actually gets called is the function
> mod1:func1(x,y,{mod1,a,b,c})
>
> So for example if we define a module sneaky.erl like this:
>
>   -module(sneaky).
>   -export([test/2, hide/1]).
>
>   test(X, Y) ->  {sneaky, X, Y}.
>
>   hide(Something) ->
>      {sneaky, Something}.
>
> Then in the shell we can do this:
>
> 1> c(sneaky).
> {ok,sneaky}
> 2> X = sneaky:hide({rectange,10,file,"foo"}).
> {sneaky,{rectange,10,file,"foo"}}
> 3> X:test(abc).
> {sneaky,abc,{sneaky,{rectange,10,file,"foo"}}}
>
> Cheers
>

Personally I love this form and use it my self.  I like parametrized
modules, but I do not use the normal decoration, I build it up myself, just
like this.  It is wonderfully obvious to me.  Although I usually have a
parent module use a helper module to handle all that, but I still do it
explicitly as it is easy for me to forget about the decorations otherwise.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120627/c09f3b2b/attachment.htm>


More information about the erlang-questions mailing list