[erlang-questions] Order of evaluation of guards
Ulf Wiger
ulf@REDACTED
Tue Jan 6 11:25:20 CET 2015
> On 06 Jan 2015, at 05:20, Richard A. O'Keefe <ok@REDACTED> wrote:
>
> If you want an “OR” to govern an “AND” you will have to use “andalso” instead
> of “,” and “orelse” instead of “;”.
…or suffer some repetition together with some careful layout.
When digging for an example, I noticed that I have been ambivalent on how to express these kinds of guards. In locks_agent.erl, for example, I found the following:
lock(Agent, [_|_] = Obj, Mode, [_|_] = Where, R)
when is_pid(Agent) andalso (Mode == read orelse Mode == write)
andalso (R == all orelse R == any orelse R == majority
orelse R == majority_alive orelse R == all_alive) ->
(yuck!)
and later:
change_flag(Agent, Option, Bool)
when is_boolean(Bool), Option == abort_on_deadlock;
is_boolean(Bool), Option == await_nodes;
is_boolean(Bool), Option == notify ->
I actually prefer the repetition, but the first example isn’t helped by that. It really could use some new syntax:
#agent(A) when is_pid(A) -> A.
#obj([_|_] = O) -> O.
#mode(M) when M==read; M==write -> M.
#where([_|_] = W) -> W.
#req(R) when R==all; R==any; R==majority; … -> R.
lock(#agent(Agent), #obj(Obj), #mode(Mode), #where(Where), #req(R)) ->
…
(EEP 29, or at least my sloppy understanding of it.)
BR,
Ulf
Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com
More information about the erlang-questions
mailing list