Booleans
Booleans are implemented with messages - they are not built-in keywords like in other languages!
true:
false:
There is no if in gab. Typically, a defcase is used instead:
my_message: .defcase! {
true: (args) => do
# Do something with args in the truthy case
end
false: (args) => do
# Do something with args in the falsey case
end
}
some_condition .my_message args
For simple use cases, there are messages and:, or:, then: and else: defined in the core library.
# Lifted from gab's core library.
truthy_values .defmodule! {
and: (alt[]) => alt**
or: _ => self
then: f => f. self
else: _ => self
}
falsey_values .defmodule! {
and: _ => self
or: (alt[]) => alt**
then: _ => self
else: f => f. self
}
The and: and or: messages do what you expect for the most part, except they don’t short circuit. This means the value on the right is always evaluated.
true: .and 2 # 2
false: .and 2 # .false
false: .or 2 # 2
true: .or 2 # .true
The then: and else: messages do short circuit, by accepting blocks instead of values.
true: .then () => do
# Do something in the truthy case
end
false: .else () => do
# Do something in the falsey case
end
This is is the part of Gab that some may find to be most inconvenient. However, I find that it encourages writing smaller functions and more modular code, as nesting lots of scopes and conditionals is impossible.