Boolean expressions

Quick examples:

if first.isJust then first else second

c && (!a || !b) == c && !(a && b)

Booleans and if

true and false (all lower case) are the values of type Boolean. if expressions have the following form:

if expression then expression else expression

The condition must be of type Boolean. There is no implicit conversion to Boolean from, for example, Integer. The types of the then and else branches must be the same.

Every if must have an else branch, there is no if-then. The else branch will extend as far as possible.

Example: To clarify by what “as far as possible” means, the following code:

 if condition
 then foo
 else bar ++ if condition2
             then foo2
             else bar2 ++ more

will parse as

 if (condition)
 then (foo)
 else (bar ++ if (condition2)
              then (foo2)
              else (bar2 ++ more))

Logical operators

The standard C-style boolean operators are present:

expression && expression
expression || expression
!expression

These operators DO short-circuit evaluation.

Again, the operands must be of type Boolean, there are no implicit conversions. The not operator binds more tightly than the and operator, which binds more tightly than the or operator, as you would expect.

Comparison operators

All comparison operators bind more tightly than logical operators and produce Boolean values.

The following comparison operators work on types Integer, Float, and String:

expression < expression
expression <= expression
expression > expression
expression >= expression
expression == expression
expression != expression

String comparison is lexicographical, and does pay attention to case. Equality and inequality will also work with Booleans, but the other comparison operators will not.