An “unless” expression is a very simple syntactic alternative to expressions of the form “if not …”.
unless e1 then e2
means the same thing as
if not(e1) then e2
Thus “unless” forms a counterpart to “until” (recall that “until e1 do e2” means “while not(e1) do e2”).
Consider :-
if not(f := open(file)) then {
handle failure
...
}
Assuming this isn’t within the context of a generator, it can also be written as :-
(f := open(file)) | {
handle failure
...
}
or using an “unless” expression :-
unless f := open(file) then {
handle failure
...
}
which has the advantage of removing the clutter of parentheses around “f := open(file)” (which are required because of the precedence of “not” and “|”), and avoiding inadvertent errors should those parentheses be omitted.
An “else” clause can be attached to the “unless” expression if desired, although it may be better to swap the two branches around, for of course :-
unless e1 then e2 else e3
means the same thing as
if e1 then e3 else e2
Contents