illegal guard expression
今天遇到一个奇怪的问题:
put(a, 1), if get(a) =:= 1 -> ok; true -> bad end. * 2: illegal guard expression
查了一下官方关于Guard的文档:
The set of valid guard expressions (sometimes called guard tests) is a subset of the set of valid Erlang expressions. The reason for restricting the set of valid expressions is that evaluation of a guard expression must be guaranteed to be free of side effects.
网上也有人问道:
On Fri, Dec 2, 2011 at 10:30 AM, Barco You <> wrote:
Why does the following expression got “illegal guard expression” when compiling:
X = 0.5,
if
random:uniform() < X -> %error reported for this line
good;
true ->
bad
end.But if I change it to following expression, it’s ok:
X = 0.5,
Ran = random:uniform(),
if
Ran < X ->
good;
true ->
bad
end.
Yes, and not only does this mean that only a limited set of functions are allowed as guard functions, random:uniform() _does_ have side effects, since it updates the seed in the process dictionary.
可见,erlang:get/1 also has side effect.