关于“if”的用法在Erlang语言 [英] About the usage of "if" in Erlang language

查看:167
本文介绍了关于“if”的用法在Erlang语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

 列表:foreach(fun(Method,Value))> 
ServerName = method_to_servername(Method),
if
Value == 0 andalso whereis(ServerName)= / = undefined - >
supervisor:terminate_child(flowrate,whereis(ServerName));
Value = / = 0 andalso whereis(ServerName)== undefined - >
supervisor:start_child(?MODULE,[M​​ethod]);
Value = / = 0 andalso whereis(ServerName)= / undefined - >
gen_server:call(method_to_servername(Method),
{update_config,
{DesAddress,Method,RateLimitList,
QueueTime,
MinRetry,MaxRetr y,Callback}});
true - > ok
end
end,?ALL_METHODS)。

当我编译代码时,我遇到这个问题: illegal guard expression ,可以给我一些建议。

解决方案

表达式称为保护序列。只有有限数量的功能被允许在保护序列中,而 whereis 不是其中之一。有关完整列表,请参阅Erlang参考手册中的Guard序列部分。 / p>

因此,大多数Erlang程序员很少使用,如果。使用 case 通常会提供更自然而简洁的代码。您的示例可以写成:

 列表:foreach(fun(Method,Value))> 
ServerName = is_pid(ServerPid) - >
supervisor:terminate_child(flowrate,ServerPid); $($)
case {Value,whereis(ServerName)}
{0,ServerPid}当$ value = / = 0 - >
supervisor:start_child(?MODULE,[M​​ethod]);
{_,ServerPid}当is_pid(ServerPid)时,b $ b {_,undefined}
gen_server:call(method_to_servername(Method),
{update_config,
{DesAddress,Method,RateLimitList,
QueueTime,
MinRetry,MaxRetry,Callback}})
_ - > ok
end
end,?ALL_METHODS)。


This is my code:

    lists:foreach(fun(Method, Value)->
                      ServerName = method_to_servername(Method),
                      if
                          Value ==0 andalso whereis(ServerName) =/= undefined ->
                              supervisor:terminate_child(flowrate, whereis(ServerName));
                          Value =/= 0 andalso whereis(ServerName) == undefined ->
                              supervisor:start_child(?MODULE, [Method]);
                          Value =/=0 andalso whereis(ServerName) =/= undefined ->
                              gen_server:call(method_to_servername(Method),
                                              {update_config,
                                               {DesAddress, Method, RateLimitList,
                                                QueueTime,
                                                MinRetry, MaxRetry, Callback}} );
                          true -> ok
                      end
              end, ?ALL_METHODS).

when i compile the code, i meet this problem :illegal guard expression, can you give me some advise.

The tests in an if expression are called guard sequences. Only a limited number of functions are allowed in guard sequences, and whereis is not one of them. See the section on Guard Sequences in the Erlang Reference Manual for the complete list.

As a consequence, most Erlang programmers rarely use if. Using case often gives more natural and concise code. Your example could be written as:

lists:foreach(fun(Method, Value)->
                  ServerName = method_to_servername(Method),
                  case {Value, whereis(ServerName)} of
                      {0, ServerPid} when is_pid(ServerPid) ->
                          supervisor:terminate_child(flowrate, ServerPid);
                      {_, undefined} when Value =/= 0 ->
                          supervisor:start_child(?MODULE, [Method]);
                      {_, ServerPid} when is_pid(ServerPid) ->
                          gen_server:call(method_to_servername(Method),
                                          {update_config,
                                           {DesAddress, Method, RateLimitList,
                                            QueueTime,
                                            MinRetry, MaxRetry, Callback}} );
                      _ -> ok
                  end
          end, ?ALL_METHODS).

这篇关于关于“if”的用法在Erlang语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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