if
if
and unless
return a value only when their condition is true.
When used as expressions, these are ? values.
These parse specially; if a block is present, it always goes with the if
, even if there's another keyword in between.
. if true 0 . if (or true false) 1 || Thanks to special parsing, this is `unless (not true) 2`, not `unless (not true 2)`. . unless not true 2 . unless and true false 3
There is no else
expression to go with if
; use cond
or case
(both below) instead.
cond
For simple conditionals, use cond
. This is like JavaScript's ternary operator.
Like anything else, the "else" value can go in a block. What if you want blocks for both "then" and "else"? Use case
.
. cond true 'then 'else . cond false 'then 'else
switch
This is just like JavaScript's.
You don't need to break
explicitly. Fallthrough is not supported.
To handle multiple cases the same way, just write multiple values on one line.
else
handles anything not covered by another case.
f = \n switch n 0 "zero" 1 2 "a small number" else "some other number" . f 0 . f 1 . f 2 . f 3
If there is no else
, an error will be thrown for an unhandled case.
(If you really want to do nothing, just write pass
in the else
block.)
focus
The focus, _
, is a good short-lived variable when you don't need a name.
There's some helpful syntax for using it:
- Many expressions implicitly assign to the focus.
fun_
is short for(fun _)
.:Type
is short for_:Type
. (See types)
case
case
is Mason's replacement JS's if ... else if ... else
.
Tests are tried from top to bottom until one matches.
Tests are each written on their own line, with the result for that test in a block next to it.
Just like switch
, a missing else
will throw an error.
An expression written on the same line as case
on the same line will become the focus. This is optional but recommended.
rate-guess = \guess case guess || implicitly, `_ = guess` <? _ 7 "Too low!" >? _ 7 "Too high!" else "You got it!" . rate-guess 6 . rate-guess 8 . rate-guess 7
\case
and \switch
create functions with a single implicit argument: _
.
rate-guess = \case =? _ 7 "You got it!" else "Off by #(- _ 7)" . rate-guess 1337 rate-guess-2 = \switch 7 "You got it!" . rate-guess-2 7