[erlang-questions] Why can't I use array:size in a guard in a method?

Richard Carlsson carlsson.richard@REDACTED
Sun Jul 28 00:02:53 CEST 2013

On 2013-07-27 20:22, Yves S. Garret wrote:
> The code:
> printArray(Array, Size, IndexToStartWith)
>      when array:size(Array) > IndexToStartWith ->
>    io:format("~w~n", [array:get(IndexToStartWith, Array)]),
>    printArray(Array, Size, IndexToStartWith + 1).
> The error:
>  > c(arrays_example).
> arrays_example.erl:23: illegal guard expression
> error
> Why not?  I could pass the size of the array along (hence the Size
> variable), but why not procure
> the length in the guard on the fly?

You can't use any remote calls (m:f(...)) in a guard. Guards must be 
side effect free, and if the function is in another module, the compiler 
can't guarantee that (in particular considering that the other module 
could be replaced at runtime with code that does something completely 


More information about the erlang-questions mailing list