erlang“非法警卫表达”在Guards中使用功能时 [英] erlang "illegal guard expression" while using function in Guards

查看:103
本文介绍了erlang“非法警卫表达”在Guards中使用功能时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码。我正在检查3个条件。您可以看到的第一个条件是,我将 xml:get_tag_attr_s(...)的输出存储在变量中,然后在if块中使用了该变量。我的问题是,如果我尝试像在其他两个条件下一样在一行中执行上述过程,则会得到错误的非法守卫表达式

I have the following code. I am checking 3 conditions. You can see for the first condition I stored the output of xml:get_tag_attr_s(...) in a variable and then used the variable within the if block. My problem is I get error illegal guard expression, if I try to do the above process in one line like I did for the other two conditions.

此外,我还从默认条件中获取了变量'_'是不受约束的。应该是同一回事。

Also, I am getting variable '_' is unbound from the default condition. It supposed to be the same thing.

有人可以解释这个问题吗?

Can somebody please explain the issue?

validate_xmpp(Packet) ->
      Type = xml:get_tag_attr_s(list_to_binary("type"), Packet),
      if
          (Type /= <<"chat">> ->
              {error, "Message type is not chat"};
          xml:get_path_s(Packet, [{elem, list_to_binary("body")}, cdata]) /= <<"">> ->
              {error, "No or empty body"};
          exml_query:path(Packet, [{element,<<"received">>},{attr,<<"xmlns">>}]) == <<"urn:xmpp:receipts">> ->
              {error, "delivery-receipts should be ignored"};
          _->
              {ok, xml:get_tag_attr_s(list_to_binary("from"), Packet)}
      end.


推荐答案

Erlang仅允许以下人员作为警卫:

Erlang allows only these to be guards:

  • The atom true
  • Other constants (terms and bound variables), all regarded as false
  • Calls to the BIFs (built-in functions) specified in table Type Test BIFs
  • Term comparisons
  • Arithmetic expressions
  • Boolean expressions
  • Short-circuit expressions (andalso and orelse)

有关更多信息,请查看 http:// www .erlang.org / doc / reference_manual / expressions.html#id83606

For more info take a look http://www.erlang.org/doc/reference_manual/expressions.html#id83606

代替使用 _ true 。您不能在如果中使用 _ ,仅在 case 语句中使用,并查看文档

Instead of _ use true. You cannot use _ in if, only in case statements, and also take a look at the docs.

这篇关于erlang“非法警卫表达”在Guards中使用功能时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆