[erlang-questions] Don't understand the error message: call to local/imported function ... is illegal in guard

zxq9@REDACTED zxq9@REDACTED
Mon Dec 24 02:56:32 CET 2018


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



More information about the erlang-questions mailing list