unless evaluates its body for side effects when a Boolean test is False, and returns Void (zero values).
Syntax
(unless ⟨test-expr⟩
⟨body-element⟩*
⟨expr⟩)
Semantics
⟨test-expr⟩must have typeBoolean, and at least one body expression is required.- If
⟨test-expr⟩evaluates toFalse, the body is evaluated sequentially as an implicitprogn(supporting multiple expressions and shorthandletbindings). - If
⟨test-expr⟩evaluates toTrue, the body is not evaluated. - Any value(s) produced by the final
⟨expr⟩of the body are discarded; theunlessexpression always has typeVoid(zero values). - When the final body expression already has type
Void, it remains in tail position if theunlessexpression is in tail position (including forrectail calls).
Examples
(coalton-toplevel
(declare checked-divide (Integer * Integer -> (Optional Integer)))
(define (checked-divide a b)
(unless (/= b 0)
(return None))
(Some (coalton/math:div a b))))
Other notes
- Because
unlessreturnsVoid(zero values) rather thanUnit, a function declared(Foo -> Unit)that ends with anunlessform should followunlesswithUnit(or be declared(Foo -> Void)). - Use
whenfor the positive counterpart.