compiler-errors - 模式匹配Erlang?

标签 compiler-errors erlang

我有一个带有if语句的代码,该语句试图让用户输入yes或no,如果用户输入的不是yes,则请求被拒绝。这是我得到的错误:

** exception error: no match of right hand side value "yes\n"
     in function  messenger:requestChat/1 (messenger.erl, line 80)

代码在这里:
requestChat(ToName) ->
    case whereis(mess_client) of
        undefined ->
            not_logged_on;
         _ -> mess_client ! {request_to, ToName},
                request_to = io:get_line("Do you want to chat?"),
                {_, Input} = request_to,
                if(Input == yes) ->
                    ok;
                    true -> {error, does_not_want_to_chat}
                end
    end.

最佳答案

在这种情况下,您可以使用 shell 程序测试为什么会出错(或转到文档)。

如果你试试:

1> io:get_line("test ").
test yes
"yes\n"
2>

您会看到io:get_line/1不会返回元组{ok,Input},而是返回以回车符结尾的简单字符串"yes\n"。这就是错误消息中报告的内容。

因此,您的代码可以修改为:
requestChat(ToName) ->
    case whereis(mess_client) of
        undefined ->
            not_logged_on;
         _ -> mess_client ! {request_to, ToName},
                if 
                    io:get_line("Do you want to chat?") == "yes\n" -> ok;
                    true -> {error, does_not_want_to_chat}
                end
    end.

但我更喜欢案例陈述
    requestChat(ToName) ->
        case whereis(mess_client) of
            undefined ->
                not_logged_on;
             _ -> mess_client ! {request_to, ToName},
                    case io:get_line("Do you want to chat?") of
                        "yes\n" -> ok;
                        "Yes\n" -> ok;
                        _ -> {error, does_not_want_to_chat}
                    end
        end.

关于compiler-errors - 模式匹配Erlang?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23575901/

相关文章:

compiler-errors - 奇怪地重复出现的通用特征模式 : overflow evaluating the requirement

erlang - 为什么这个变量没有被使用?

flash - 创建网络视频聊天最有效的方法是什么?

c++ - 错误代码Visual Studio错误:c2059 strange syntax error

recursion - Erlang:带有未优化尾调用的递归函数的stackoverflow?

erlang - 为什么我的主管在初始化时失败并带有 badarg?

erlang - 运行牛仔应用程序时出错

permissions - 为什么即使许可为777,WHMCS在应用其安全提示后仍显示许可错误?

c++ - 编译器错误预期嵌套名称说明符

iphone - libFlurryAnalytics.a错误