[erlang-questions] Avoiding boilerplate code when using gen_server
Garrett Smith
g@REDACTED
Wed Mar 21 20:28:49 CET 2012
On Wed, Mar 21, 2012 at 2:21 PM, Joe Armstrong <erlang@REDACTED> wrote:
> On Wed, Mar 21, 2012 at 4:29 PM, Garrett Smith <g@REDACTED> wrote:
>>
>> Here's a "gen_server" equivalent in e2:
>>
>> https://github.com/gar1t/e2v2/blob/master/examples/ping/src/ping_server.erl
>
> That's nice - but one could with a little thought and a few conventions
> make this even shorter. Something like:
>
> -module(ping_service).
> -behavior(e2_service).
> -interface([ping/0]).
>
> ping(_From, State) -> {reply, pong, State}.
>
> And just let all undefined values default to sensible values.
> As in your version you could use
>
> init() -> State to prime the state
>
> A counter would just be:
>
> -module(counter).
> -behaviour(e2_service).
> -interface([inc/1, dec/1, get/0])
> init() -> 0.
>
> deposit(_From, X,N) -> {reply,ack,N+X}.
> withdraw(_From,N) ->
> {reply,ack,N-1}.
> get(_From, N) -> {reply,N,N}.
Joe, in this case, what would the "interface" documentation look like?
Garrett
More information about the erlang-questions
mailing list