I need to achieve in a cl-loop
the following conditional written in pseudocode.
if (flag1) {
if (flag2) {return ...}
} else {
return ...
}
Note that it is equivalent to
if (flag1 && flag2) {return ...}
if (!flag1) {return ...}
which undesirably repeats flag1
twice.
What I have done is:
(cl-loop ...
if (flag) do 'nothing and
if (...) return ... end
else
return ...)
The do 'nothing and
looks a bit hacky to me, but I cannot omit it as it's part of the nested if syntax. Are there alternative (and clearer) ways to do this ? Maybe using cl-block
or by outsourcing the conditionals to another function ?
EDIT (further clarification):
The use of the keywords and
and end
could help to disambiguate dangling else (which is the conditional pattern given in this question).
Inspired from @John Kitchin's example, consider the following snippets and note the indentation:
(cl-loop
with flag1 = nil
with flag2 = t
if flag1 return 3
if flag2 return 1
else return 2)
returns 1
.
On the other hand,
(cl-loop
with flag1 = nil
with flag2 = t
if flag1 return 3 and
if flag2 return 1 end
else return 2)
returns 2
.
Thus, to accomplish the desired conditional, the keywords and
and end
are required.
Now, going back to the initial question: how to achieve the latter conditional, without a clause at the return 3
position, ignoring potential infinite loops ?
It seems impossible per se: according to the documentation, the <clause>
in if <condition> <clause>
should be an accumulation, do
, return
, if
, or unless
clause.
My solution is replacing it with do 'nothing
or do (ignore)
to fulfill the documentation's requirement, or use the equivalent yet repetitive conditional (undesired, see above), but I'm looking for alternatives.