Don't understand the error message: call to local/imported function ... is illegal in guard

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Don't understand the error message: call to local/imported function ... is illegal in guard

Donald Steven
This code works:

     AlphaNumericChar = isAlphaNumeric(C),
     if AlphaNumericChar ->

but this code (which is functionally identical):

     if isAlphaNumeric(C) ->

produces an error message: call to local/imported function
isAlphaNumeric/1 is illegal in guard

Why?

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
by
Reply | Threaded
Open this post in threaded view
|

Re: Don't understand the error message: call to local/imported function ... is illegal in guard

by
Only limited expressions are permitted after “if”(guard), and the user defined function does not in the list.
I believe there is a list on Erlang Doc about what is permitted.

By the way, this limitation is for avoiding side effects when evaluating expressions.

Yao

> 在 2018年12月24日,08:26,Donald Steven <[hidden email]> 写道:
>
> This code works:
>
>     AlphaNumericChar = isAlphaNumeric(C),
>     if AlphaNumericChar ->
>
> but this code (which is functionally identical):
>
>     if isAlphaNumeric(C) ->
>
> produces an error message: call to local/imported function isAlphaNumeric/1 is illegal in guard
>
> Why?
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Don't understand the error message: call to local/imported function ... is illegal in guard

zxq9-2
In reply to this post by Donald Steven
On 2018年12月23日日曜日 19時26分47秒 JST Donald Steven wrote:

> This code works:
>
>      AlphaNumericChar = isAlphaNumeric(C),
>      if AlphaNumericChar ->
>
> but this code (which is functionally identical):
>
>      if isAlphaNumeric(C) ->
>
> produces an error message: call to local/imported function
> isAlphaNumeric/1 is illegal in guard
>
> Why?

The context of the call.

In a function head, specifically, you are only allowed to use a small set of pre-defined built-in functions. Using your own functions in guards (or anything outside the set of legal guard functions) is illegal.



  foo(Args) when tuple_check(Args) ->
          blah();
  foo(_) ->
          blee().


  tuple_check(Args) when is_tuple(Args) ->
          true;
  tuple_check(_) ->
          false.


The above definition of foo/1 will fail to compile because use of my own defined function tuple_check/1 is illegal.

The second version is legal because is_tuple/1 is a BIF that is on the whitelist of functions permitted in guards.


  foo(Args) when is_tuple(Args) ->
          blah();
  foo(_) ->
          blee().


Note that when we say "permitted in guards" we are only talking about guards in function heads. You can do whatever you want in guards in `case` statements.

The doc reference for guards has a list of the legal BIFs:
http://erlang.org/doc/reference_manual/expressions.html#guard-sequences


-Craig
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Don't understand the error message: call to local/imported function ... is illegal in guard

Donald Steven
In reply to this post by by
Thanks Yao.

On 12/23/2018 8.02 PM, by wrote:

> Only limited expressions are permitted after “if”(guard), and the user defined function does not in the list.
> I believe there is a list on Erlang Doc about what is permitted.
>
> By the way, this limitation is for avoiding side effects when evaluating expressions.
>
> Yao
>
>> 在 2018年12月24日,08:26,Donald Steven <[hidden email]> 写道:
>>
>> This code works:
>>
>>      AlphaNumericChar = isAlphaNumeric(C),
>>      if AlphaNumericChar ->
>>
>> but this code (which is functionally identical):
>>
>>      if isAlphaNumeric(C) ->
>>
>> produces an error message: call to local/imported function isAlphaNumeric/1 is illegal in guard
>>
>> Why?
>>
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Don't understand the error message: call to local/imported function ... is illegal in guard

Guilherme Andrade
In reply to this post by Donald Steven
Only certain built-in functions may be used in if clauses, function guards, case clause guards, and so on. They are listed under 'Expressions'[1], on the reference manual.

What I believe you would like to do is usually achieved using case expressions, which allow the evaluation of regular function calls. 'If' clauses are uncommon nowadays and it's rare for them to be considered more appropriate than the equivalent case clause or function head pattern.

On Mon, 24 Dec 2018 at 00:26, Donald Steven <[hidden email]> wrote:
This code works:

     AlphaNumericChar = isAlphaNumeric(C),
     if AlphaNumericChar ->

but this code (which is functionally identical):

     if isAlphaNumeric(C) ->

produces an error message: call to local/imported function
isAlphaNumeric/1 is illegal in guard

Why?

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


--
Guilherme

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Don't understand the error message: call to local/imported function ... is illegal in guard

Donald Steven

Thanks so much!  I hadn't seen  that.

On 12/24/2018 4:45 AM, Guilherme Andrade wrote:
Only certain built-in functions may be used in if clauses, function guards, case clause guards, and so on. They are listed under 'Expressions'[1], on the reference manual.

What I believe you would like to do is usually achieved using case expressions, which allow the evaluation of regular function calls. 'If' clauses are uncommon nowadays and it's rare for them to be considered more appropriate than the equivalent case clause or function head pattern.

On Mon, 24 Dec 2018 at 00:26, Donald Steven <[hidden email]> wrote:
This code works:

     AlphaNumericChar = isAlphaNumeric(C),
     if AlphaNumericChar ->

but this code (which is functionally identical):

     if isAlphaNumeric(C) ->

produces an error message: call to local/imported function
isAlphaNumeric/1 is illegal in guard

Why?

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


--
Guilherme

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions