Parameterized/Abstract modules

Raimo Niskanen raimo@REDACTED
Tue Mar 8 12:46:44 CET 2005


[Dan]:

I took a quick glance at it today, and thought that I should
be able to do something like:

-module(api, [Server]).
-export([start/0,foo/1]).

start() ->
     new(spawn(fun() -> init() end)).

foo(Arg) ->
   call(foo,Arg).

call(Op,Args) ->
    Server ! {Op, Args},
    receive {Server,Res} -> Res end.


I.e. I want to have functions that can be called without being
parameterized (sp?).

I can implement this with two modules the first one is not a
parameterized module, it spawns a process or does other init work,
calls new in the real parameterized module, but I thought it was a
ugly workaround and it also HIDES the api from the user.

Raimo and I talked about it here during lunch and came up with the
following suggestions.

A new export list, for functions that should not be parameterized,
which can be called directly, e.g. api:start() or api:help().

-non_parameterized_exports([start/0,help/0]).

and a macro or bif which is called at the end of the "constructors".

start() ->
   Pid = spawn(fun() ->..end),
   erlang:create_parameterized_module(?MODULE, [Pid]).
   %% or 
   ?MAKE_PARAMETERIZED_MODULE([Pid]).

That way the programmer can decide the name of the "constructor" it 
doesn't have to be new(..) with exactly the same number of arguments
as module parameters and in the same order, it could be named init, start
or creat :-)

For backwards compability a new(Params) function can be generated if 
-non_parameterized_exports() is not present. But do we really have to
be backwards compatible, is anyone using it alot?

Comments?

Name suggestions?

/Dan and Raimo


-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list