【问题标题】:Erlang syntax error before: 'end'之前的 Erlang 语法错误:'end'
【发布时间】:2014-03-19 23:20:52
【问题描述】:

我最近开始学习erlang,但是遇到了一个让我很困惑的错误。

最后一行的错误是syntax error before: 'end'。我查看了示例并试图找到错误,但此刻我完全迷失了。有什么想法吗?

ChannelToJoin = list:keysearch(ChannelName,1,State#server_st.channels),
case ChannelToJoin of
    % Channel exists.
    {value, Tuple} ->
        if 
            %User is not a member of the channel
            not list:member(UserID, Tuple) ->
            %Add the user to the channel
            Tuple#channel.users = list:append(Tuple#channel.users, [UserID]);


            % If the user is already a member of the channel.
            true -> true
        end;
    %Channel doesn't exist
    false ->
        %Create new channel and add the user to it.
        NewState = State#server_st{channels = list:append(State#server_st.channels, NewChannel = #channel{name = ChannelName, users = [UserID]}
end

【问题讨论】:

  • 你能发布更多代码吗?这对我来说看起来不错,除了您尝试更新if 中的Tuple 记录。记住:erlang 中的变量是只读的

标签: erlang


【解决方案1】:

倒数第二行 NewState = ... 缺少两个右括号:)}

还请注意,您不能在if 中使用lists:member,因为在保护表达式中不允许函数调用(这是if 允许您使用的)。相反,请使用case:

case lists:member(UserID, Tuple#channel.users) of
    false ->
         %% Add the user to the channel
         ...;
    true ->
         %% Already a member
         ok
end

【讨论】:

  • 干杯,这真的很有帮助!
猜你喜欢
  • 2018-12-22
  • 2011-04-06
  • 2014-06-08
  • 2015-02-24
  • 2022-01-12
  • 2015-01-27
  • 2019-03-09
  • 2012-05-16
  • 1970-01-01
相关资源
最近更新 更多