Erlang gen_服务器强制转换错误的返回值
我尝试将消息强制转换到gen_服务器:Erlang gen_服务器强制转换错误的返回值,erlang,gen-server,Erlang,Gen Server,我尝试将消息强制转换到gen_服务器: gen_server:cast({global, ID}, {watchers}). 处理程序是: handle_cast({watchers}, State) -> case State#table_state.watchers of [] -> {reply, no_watchers, State}; _ -> {reply, State#table_state.watche
gen_server:cast({global, ID}, {watchers}).
处理程序是:
handle_cast({watchers}, State) ->
case State#table_state.watchers of
[] ->
{reply, no_watchers, State};
_ ->
{reply, State#table_state.watchers, State}
end;
但是当我执行gen\u server:cast
时,gen\u服务器会以错误终止:
=ERROR REPORT==== 29-Apr-2011::18:26:07 ===
** Generic server 1 terminating
** Last message in was {'$gen_cast',{watchers}}
** When Server state == {table_state,1,"1",11,[]}
** Reason for termination ==
** {bad_return_value,{reply, no_watchers, {table_state,3,"3",11,[]}}}
为什么我会得到
错误的返回值?您无法使用cast回复(请参阅)。这就是强制转换异步消息而不是使用调用的全部要点
在您的情况下,您希望返回一个回复,因此使用gen\u server:call/2
。作为旁注,您不必发送{watchers}
(元组中的一个原子),只需将watchers
原子作为消息发送就足够了。因此handle\u cast
应该返回{noreply,State}
。对于返回值的同步调用,请使用gen\u server:call
和handle\u call
。