Parameterized module idioms
Mark Fine
mark.fine@REDACTED
Fri Apr 16 20:09:34 CEST 2010
Is there a cleaner, more idiomatic way to use object-like parameterized
modules:
-module(echo, [PID]).
-behaviour(gen_server).
-export([start_link/0, echo/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).
start_link() ->
case gen_server:start_link(?MODULE:new(undefined), [], []) of
{ok, Pid} ->
{ok, ?MODULE:new(Pid)};
Else ->
Else
end.
echo(What) ->
gen_server:call(PID, {echo, What}).
handle_call({echo, What}, _From, State) ->
{reply, What, State}.
init([]) -> {ok, []}.
handle_cast(_Msg, _State) -> undefined.
handle_info(_Info, _State) -> undefined.
terminate(_Reason, _State) -> undefined.
code_change(_OldVsn, _State, _Extra) -> undefined.
17> {ok, E} = (echo:new(undefined)):start_link().
{ok,{echo,<0.82.0>}}
18> E:echo("hello, world").
"hello, world"
19>
More information about the erlang-questions
mailing list