Operators in Gleam
Learn arithmetic, comparison, logical, and pipe operators in Gleam, including the separate operators for integers and floats
Operators are the building blocks of expressions. Gleam’s operator set reflects its functional, statically-typed nature: every operator has a single, well-defined type signature, and the compiler will refuse to compile expressions that mix incompatible types.
The most distinctive aspect of Gleam’s operators is the strict separation between integers and floats. Unlike most languages, you cannot use + to add two floats together. Instead, Gleam provides a parallel family of dot-suffixed operators (+., -., *., /.) specifically for Float values. This design eliminates an entire class of numeric precision bugs at compile time.
Beyond arithmetic, Gleam offers a string concatenation operator (<>), boolean logic (&&, ||, !), and the pipe operator (|>) that is central to idiomatic Gleam code. There are no compound assignment operators like += because Gleam values are immutable — you cannot mutate a binding in place.
Integer and Float Arithmetic
Gleam separates integer and float arithmetic to keep numeric semantics unambiguous. Mixing them is a compile error, not a silent coercion.
Create a file named operators.gleam:
| |
Note how int.to_string and float.to_string are required to combine numbers into a string — there is no automatic numeric-to-string coercion. Also notice that 10 / 0 evaluates to 0 rather than crashing; Gleam deliberately makes division total to avoid runtime errors.
Comparison and Boolean Logic
Equality works across any type with == and !=, but the ordering operators (<, >, <=, >=) are integer-specific. Floats have their own dotted ordering operators (<., >., <=., >=.).
Create a file named comparisons.gleam:
| |
Gleam has no concept of “truthy” or “falsy” values. The boolean operators only accept actual Bool values (True or False), so writing if some_string is impossible. You must always produce an explicit boolean.
String Concatenation and the Pipe Operator
The <> operator concatenates strings. The |> pipe operator is one of Gleam’s most idiomatic features — it threads a value through a series of function calls, passing the value as the first argument to each function.
Create a file named pipes.gleam:
| |
Reading the piped version aloud — “start with hello, reverse it, then uppercase it” — matches how the computation actually proceeds, which is why Gleam programmers reach for |> so often.
Running with Docker
Gleam needs a project structure, so the Docker command creates one on the fly and copies your source file into it.
| |
Expected Output
Running operators.gleam produces:
Integer arithmetic:
7 + 3 = 10
7 - 3 = 4
7 * 3 = 21
7 / 3 = 2
7 % 3 = 1
Float arithmetic:
7.0 +. 3.0 = 10.0
7.0 -. 3.0 = 4.0
7.0 *. 3.0 = 21.0
7.0 /. 2.0 = 3.5
10 / 0 = 0
Running comparisons.gleam produces:
5 == 5 -> True
"yes" != "no" -> True
3 < 7 -> True
10 >= 10 -> True
3.5 <. 7.2 -> True
9.9 >=. 9.9 -> True
True && False -> False
True || False -> True
!True -> False
5 > 3 && 2 < 4 -> True
Running pipes.gleam produces:
Hello, Gleam!
Nested: OLLEH
Piped: OLLEH
Sum: 15
2 + 3 * 4 > 10 && 1 < 2 -> True
Key Concepts
- Separate operators for Int and Float —
+ - * / %work only on integers;+. -. *. /.work only on floats. Mixing types is a compile error, not a runtime surprise. - Total arithmetic — Integer division by zero returns
0rather than raising an exception, keeping all arithmetic functions total. - No truthy values —
&&,||, and!accept onlyBool. There is no implicit conversion from numbers, strings, or lists to booleans. - String concatenation uses
<>— The+operator does not concatenate strings; it is reserved for integer addition. - No compound assignment — Operators like
+=do not exist because bindings are immutable. Useletto bind a new value. - The pipe operator
|>— Threads a value as the first argument through a chain of function calls, replacing nested calls with a top-to-bottom flow. - Ordering on floats is dotted too — Use
<.,>.,<=.,>=.to compare floats; the bare ordering operators are integer-only. - Equality is generic —
==and!=work on any type, but both sides must have the same type or the compiler will reject the expression.
Running Today
All examples can be run using Docker:
docker pull ghcr.io/gleam-lang/gleam:v1.14.0-erlang-alpine
Comments
Loading comments...
Leave a Comment