erlang 为什么我的gen_server不支持模式匹配?

umuewwlo  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(154)

Hello I am trying to figure out why my implementation of gen_server is not respecting pattern matching:
If I run gen_server:call(ServerRef,state) it gets in the second pattern of handle_call and i do not understand why , since the the first pattern should get hit.
Is there a problem when sending atoms ?

Module

-module(wk).
-behaviour(gen_server).
-compile(export_all).

-record(state,{
   limit,
   processed=[],
   unknown=[],
   counter=0
}).
start_link(Limit)->
   gen_server:start_link(?MODULE, Limit, []).

start(Limit)->
    gen_server:start(?MODULE,Limit,[]).
init(Limit)->
    State=#state{limit=Limit},
    {ok,State}.

handle_call(From,state,State)->
    {reply,State,State};
handle_call(From,Message,State=#state{processed=P,limit=L,counter=C})->
     Reply=if C=:=L;C>L -> exit(consumed);
              C<L       -> {{processed,self(),os:timestamp()},Message}
     end,
    {reply,Reply,State#state{counter=C+1,processed=[Message,P]}}.

handle_info(Message,State=#state{unknown=U})->
    {noreply,State#state{unknown=[Message|U]}}.

Calling:
>gen_server:call(ServerRef,state) enters into the second pattern too.

i1icjdpr

i1icjdpr1#

因为参数顺序错误:它应该是Module:handle_call(Request, From, State)。因此第一个模式只有在Fromstate时才匹配。

相关问题