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

Nick Gerakines nick@REDACTED
Fri Oct 30 21:05:19 CET 2009


Yeah, not only can you have param'd modules but they can be extensions
of each other.

http://gist.github.com/149735

--- begin nick.erl
-module(nick, [City, State]).

-export([city/0, state/0]).

state() ->
    io:format("nick:state/0 -> ~p~n", [State]).

city() ->
    io:format("nick:city/0 -> ~p ~n", [City]).
--- end

--- begin vanessa.erl
-module(vanessa, [City, State]).
-extends(nick).
-export([city/0]).

city() ->
    io:format("child:city/0 -> ~p ~n", [City]).
--- end
$ erlc nick.erl && erlc vanessa.erl

1> Me = nick:new("Mountain View", "CA").
{nick,"Mountain View","CA"}
2> Me:city().
nick:city/0 -> "Mountain View"
ok
3> Me:state().
nick:state/0 -> "CA"
ok
4> Vanessa = vanessa:new("Mountain View", "CA").
{vanessa,{nick,"Mountain View","CA"},"Mountain View","CA"}
5> Vanessa:city().
child:city/0 -> "Mountain View"
ok
6> Vanessa:state().
nick:state/0 -> "CA"
ok
7>

On Fri, Oct 30, 2009 at 9:51 AM, Greg Smyth <gsmyth@REDACTED> wrote:
> Looking at your example, I don't think this is parameterised modules
> (although that is also interesting, thanks!).
>
> The confusing part (for me at least), is still the section:-
>
>> % 14> Dog = dog:new("Fido").
>> % ok
>>
>> % 15> Dog:woof().
>> % "Fido" says 'woof!'
>
> How does Value:function()  - change into
>
> module:function(Value)
>
> when Value = {module, Something}) ?
>
> Cheers,
> Greg
>
> On Fri, Oct 30, 2009 at 4:42 PM, Dale Harvey <harveyd@REDACTED> wrote:
>> The syntax is a bit wrong,
>> -module(dog, [Name]).
>> -export([woof/1]).
>> woof(Self) ->
>>  {dog, Name} = Self,
>>  io:format("~p says 'woof!'~n", [Name]).
>>
>> % 13> c(dog).
>> % {ok,dog}
>>
>> % 14> Dog = dog:new("Fido").
>> % ok
>>
>> % 15> Dog:woof().
>> % "Fido" says 'woof!'
>> % ok
>> but erlang does support it, parameterised modules
>> http://www.clickcaster.com/channel/tag/modules?channel=diveintoerlang
>> 2009/10/30 Dave Smith <dizzyd@REDACTED>
>>>
>>> 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 <gsmyth@REDACTED>
>>> 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
>>>
>>
>>
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>


More information about the erlang-questions mailing list