我正在尝试将列表ConnectedAtoms分配给新创建的server_st记录中的列表connectedatoms。但是我得到了一个糟糕的错误。
-module(server).
-export([loop/2, initial_state/1]).
-include_lib("./defs.hrl").
loop(St,{tryConnect,Atom}) ->
IsConnected = funcs:hasElem(St#server_st.connectedatoms,Atom),
if
IsConnected==true ->
{'EXIT',user_already_connected};
IsConnected==false ->
ConnectedAtoms = St#server_st.connectedatoms ++ [Atom],
St = #server_st{servername=St#server_st.servername,connectedatoms=ConnectedAtoms},
{"Connected!",St}
end;
loop(St, _Msg) ->
io:format("Server got message.~n"),
{ok, St}.
initial_state(_Server) ->
#server_st{servername=_Server,connectedatoms=[]}.
答案 0 :(得分:6)
当您尝试将值重新分配给St.时,会出现错误匹配错误。由于Erlang是一种函数式编程语言,因此一旦为变量赋值,就无法更改该值。一旦变量绑定到某个值,接下来'='运算符只匹配模式。
St = #server_st{servername=St#server_st.servername,connectedatoms=ConnectedAtoms},
将其更改为其他变量
StConnected = #server_st{servername=St#server_st.servername,connectedatoms=ConnectedAtoms},