[erlang-questions] Interesting style i haven't seen....

Bob Ippolito <>
Fri Oct 30 19:27:20 CET 2009


That's how parameterized modules work. An instance of a parameterized
module is a variable.

This particular example is (ab)using an implementation detail of how
parameterized modules are currently implemented. You'd get the same
behavior with this module, which uses an actual parameterized module:

----

-module(dog, [Name]).
-export([woof/0]).

woof() ->
 io:format("~p says 'woof!'~n", [Name]).

----

1> c(dog).
{ok,dog}
2> Fido = dog:new(fido).
{dog,fido}
3> Fido:woof().
fido says 'woof!'
ok

On Fri, Oct 30, 2009 at 12:36 PM, Chris Tilt <> wrote:
> It's the use of Dog:woof() that throws me. Dog is a variable and I don't understand the semantics of "Variable:function()".
> Cheers, Chris
>
>
> On 10/30/09 9:32 AM, "Dave Smith" <> wrote:
>
> Is this a trick question? I feel like I'm missing something here... :)
>
> If the confusion is re: the use of "new", remember that Erlang has no
> concept of objects or explicit allocation (like that anyways).
>
> D.
>
> On Fri, Oct 30, 2009 at 10:25 AM, Greg Smyth <> wrote:
>> A friend just came up with this, and i thought it was pretty
>> interesting(arguments against doing this kind of thing in Erlang
>> aside...) as I'd never seen this type of thing done before...
>>
>> Anyone care to shed some light on why/how this works:-
>>
>> %%
>> -module(dog).
>> -export([new/1, woof/1]).
>>
>> new(Name) -> {dog, Name}.
>>
>> woof(Self) ->
>>  {dog, Name} = Self,
>>  io:format("~p says 'woof!'~n", [Name]).
>> %%
>>
>> % 13> c(dog).
>> % {ok,dog}
>>
>> % 14> Dog = dog:new("Fido").
>> % {dog,"Fido"}
>>
>> % 15> Dog:woof().
>> % "Fido" says 'woof!'
>> % ok
>>
>> Many thanks,
>> Greg
>>
>> ________________________________________________________________
>> erlang-questions mailing list. See http://www.erlang.org/faq.html
>> erlang-questions (at) erlang.org
>>
>>
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>
>


More information about the erlang-questions mailing list