Booleans
In Gleam, boolean literal is either True
or False
. Its type is Bool
. Here are some basic operations with booleans:
False && False // => False
False && True // => False
True && False // => False
True && True // => True
False || False // => False
False || True // => True
True || False // => True
True || True // => True
&&
and ||
are short circuiting, meaning they don't evaluate the right hand side if they don't have to.
&&
evaluates the right hand side if the left hand side is True
. So it is literally a name for "logical AND". So it returns True
only if all two inputs are True
.
||
evaluates the right hand side if the left hand side is False
. So it is literally a name for "logical OR". So it returns False
only if all two inputs are False
.
If you want to print boolean with io.println
function, you need to first convert it to String
using bool.to_string
:
import gleam/io
import gleam/bool
pub fn main() {
io.println(
bool.to_string(True),
)
}
Output:
True
Note: you can alternatively use
io.debug
, if you want to print any type you want:import gleam/io pub fn main() { io.debug(False) io.debug("True") }
Output:
False True
Gleam also supports negation of Bools using either the !
operator or the bool.negate
function from the gleam/bool
module:
import gleam/io
import gleam/bool
pub fn main() {
io.debug(!True)
io.debug(bool.negate(False))
}
Output:
False
True
Important:
&&
and||
are short circuiting, meaning they don't evaluate the right hand side if they don't have to. Example:import gleam/io fn test() -> Bool { io.println("test") True } pub fn main() { io.debug(False && test()) }
Output:
False
Here
"test"
wasn't printed, because no matter whattest()
returns, the result of the&&
operator isFalse
. However, if we will changeFalse
toTrue
:import gleam/io fn test() -> Bool { io.println("test") True } pub fn main() { io.debug(True && test()) }
"test"
will be printed:test True
Note: calls like this:
io.debug(True)
Can be simplified, using the pipe operator (
|>
):True |> io.debug
Another example:
import gleam/io import gleam/bool pub fn main() { True |> bool.to_string |> io.println }