DEV Community

Cover image for Gleam Tutorial: Type-Safe Code That Runs on the BEAM
The Practical Developer
The Practical Developer

Posted on Originally published at practicaldevblog.blogspot.com

Gleam Tutorial: Type-Safe Code That Runs on the BEAM

Quick answer

Gleam is a statically typed functional language that runs on the BEAM, the virtual machine that powers Erlang and Elixir. You write small functions with types the compiler checks, and the BEAM runs them as lightweight processes that can fail and restart. This post builds a real order calculator for a small Karachi clothing shop: items go in with prices in rupees, the total comes out checked by the compiler, and a test proves the maths.

A small clothing shop in Karachi takes orders over WhatsApp. The owner reads each message, copies the items into a notebook, and works out the total on a calculator. It works until it does not: one order copied with the wrong quantity, one total added up wrong, one customer asking whether her order went through while nobody can answer. The shop needs one place where an order is recorded once and checked as it arrives. A typed language on the BEAM fits that job, because the compiler checks the numbers before any customer sees them.

What I would check before using it

My first check is whether the BEAM is in the picture. Gleam compiles to Erlang bytecode, runs on the same virtual machine as Erlang and Elixir, and can call the same Erlang libraries. Without BEAM experience, it is only half the learning. The Erlang Explained: Processes That Fail and Restart post covers what that runtime gives you.

My second check is the size of the library shelf. Gleam reached 1.0 in March 2024, so it is young and its package list is far smaller than Elixir's. Check the packages the project needs exist first. Gleam can call Erlang code directly, so a missing library is not a dead end, but building that bridge is work.

My third check is which target the project needs. Gleam compiles to Erlang and to JavaScript. This post uses the Erlang target, which gets the BEAM's processes and fault tolerance. The JavaScript target fits browser code, but the BEAM benefits stay behind.

My fourth check is how the team feels about strict types. Gleam draws a hard line between integers and floats: + adds integers, +. adds floats, and mixing them is a compile error. That strictness catches real mistakes, at the cost of ceremony.

My verdict, as judgement: adopt Gleam for a new BEAM service where correctness matters and the team can learn a young language. It flips when the project needs a library that only exists in Elixir, or the team has no time to learn the runtime too.

A Karachi clothing shop, totalled by Gleam

The demo is the Karachi clothing shop from the intro: three items, a total worked out by the program, and a test.

Install Gleam from the official binary release:

$ ./gleam --version
gleam 1.19.2
Enter fullscreen mode Exit fullscreen mode

Gleam needs an Erlang runtime:

$ erl -version
Erlang (SMP,ASYNC_THREADS) (BEAM) emulator version 15.2.7.15
Enter fullscreen mode Exit fullscreen mode

Create the project:

$ gleam new shop_orders
Your Gleam project shop_orders has been successfully created.
The project can be compiled and tested by running these commands:

    cd shop_orders
    gleam test
Enter fullscreen mode Exit fullscreen mode

Code lives in src/shop_orders.gleam, tests in test/shop_orders_test.gleam. Here is the module: an order item with a name, a rupee price, and a quantity, and a function that totals a list:

import gleam/int
import gleam/io
import gleam/list

pub type OrderItem {
  OrderItem(name: String, price_rs: Int, qty: Int)
}

pub fn item_total(item: OrderItem) -> Int {
  item.price_rs * item.qty
}

pub fn order_total(items: List(OrderItem)) -> Int {
  items
  |> list.map(item_total)
  |> int.sum
}

pub fn main() {
  let items = [
    OrderItem("White Cotton Kurta", 2450, 1),
    OrderItem("Navy Blue Shalwar Kameez", 3200, 2),
    OrderItem("Pastel Lawn Kurta", 1950, 2),
  ]
  let total = order_total(items)
  io.println("Order total: Rs " <> int.to_string(total))
}
Enter fullscreen mode Exit fullscreen mode

Run it. The first run downloads the standard library and the test package:

$ gleam run
  Compiling gleam_stdlib
  Compiling gleeunit
  Compiling shop_orders
   Compiled in 1.64s
    Running shop_orders.main
Order total: Rs 12750
Enter fullscreen mode Exit fullscreen mode

Rs 2,450 once plus Rs 3,200 twice plus Rs 1,950 twice comes to Rs 12,750. Now the shop adds a delivery fee, and my first attempt wrote it as a float, 150.0, added straight to the integer total:

  let total = order_total(items) + 150.0
Enter fullscreen mode Exit fullscreen mode

The compiler refused:

error: Type mismatch
   ┌─ /home/hatch/workspace/.toolchains/verify/batch67-gleam/shop_orders/src/shop_orders.gleam:25:36
   │
25 │   let total = order_total(items) + 150.0
   │                                    ^^^^^

The + operator expects arguments of this type:

    Int

But this argument has this type:

    Float

Hint: The +. operator can be used with Floats
Enter fullscreen mode Exit fullscreen mode

That is the type system doing its job: a rupee total is an integer here, and a float fee does not belong in it. The fix is to keep money in whole rupees, so the fee becomes the integer 150:

pub fn main() {
  let items = [
    OrderItem("White Cotton Kurta", 2450, 1),
    OrderItem("Navy Blue Shalwar Kameez", 3200, 2),
    OrderItem("Pastel Lawn Kurta", 1950, 2),
  ]
  let total = order_total(items)
  let with_delivery = total + 150
  io.println("Order total: Rs " <> int.to_string(total))
  io.println("With delivery: Rs " <> int.to_string(with_delivery))
}
Enter fullscreen mode Exit fullscreen mode

Run it again:

$ gleam run
  Compiling shop_orders
   Compiled in 0.57s
    Running shop_orders.main
Order total: Rs 12750
With delivery: Rs 12900
Enter fullscreen mode Exit fullscreen mode

The test pins the total down. Replace the generated test with one that totals the same three items:

import gleeunit
import shop_orders

pub fn main() -> Nil {
  gleeunit.main()
}

// gleeunit test functions end in `_test`
pub fn order_total_test() {
  let items = [
    shop_orders.OrderItem("White Cotton Kurta", 2450, 1),
    shop_orders.OrderItem("Navy Blue Shalwar Kameez", 3200, 2),
    shop_orders.OrderItem("Pastel Lawn Kurta", 1950, 2),
  ]

  assert shop_orders.order_total(items) == 12_750
}
Enter fullscreen mode Exit fullscreen mode
$ gleam test
  Compiling shop_orders
   Compiled in 0.54s
    Running shop_orders_test.main
.
1 passed, no failures
Enter fullscreen mode Exit fullscreen mode

One test, one pass. If a price or a quantity changes, the test fails before the wrong total reaches a customer.

Terminal output showing a Gleam type error on adding an Int to a Float, then the fixed program printing Rs 12,750 and Rs 12,900 with delivery.

Common mistakes and how to fix them

First, adding an integer to a float with +. The compiler answers: "The + operator expects arguments of this type: Int. But this argument has this type: Float." The fix is one type for money: integers for whole rupees, or +. for floats.

Second, calling int.sum with no import gleam/int. The compiler answers: "No module has been found with the name int", and hints "Did you mean to import gleam/int?" The fix is the import line.

Third, running Gleam without Erlang installed. The error is: "The program escript was not found. Is it installed?" The fix is to install Erlang/OTP 27 or newer, then check erl -version.

Fourth, calling a function from another module that is not marked pub. The compiler answers: "shop_orders does define order_total, but it is private and cannot be accessed by other modules." The fix is pub, as this post does with order_total.

Fifth, a value of the wrong type in a record field, like a string price in an OrderItem. The compiler answers: "Expected type: Int. Found type: String." The fix is at the data: prices are integers, so send 2450, not "2450".

Frequently Asked Questions

Is Gleam the same as Elixir? No. They share the BEAM, but Gleam is statically typed, Elixir dynamically typed: Gleam checks types at compile time, Elixir catches type problems at run time. The Phoenix Explained: Real-Time Web Apps That Never Stop post shows Elixir's web framework.

Do I need to know Erlang to use Gleam? No, you can write Gleam without a line of Erlang. Knowing how the BEAM works helps, especially processes and supervision, and the Erlang Explained: Processes That Fail and Restart post covers that.

Can Gleam code run in the browser? Yes. Gleam compiles to JavaScript as well as Erlang, so the same language runs on a server and in a browser. This post uses the Erlang target, which gets the BEAM's fault tolerance.

Is Gleam ready for production? It reached 1.0 in March 2024 and is used in production by some teams, but its library shelf is still smaller than Elixir's. My judgement: check the packages the project needs exist first, and keep the first Gleam service small.

What to remember

  • Gleam is statically typed: the compiler checks every value before the code runs.
  • Int and Float are different types: + adds integers, +. adds floats, and mixing them fails to compile.
  • The BEAM gives you lightweight processes and fault tolerance, the runtime Erlang and Elixir share.
  • gleam new, gleam run, and gleam test cover the loop.
  • Keep money in whole rupees as integers, and the type system checks the arithmetic.

Where this leaves you

One small module, three order items, and the shop now has a total the compiler checked and a passing test to pin it down: Rs 12,750, or Rs 12,900 with delivery. The type error was the useful part: the compiler would rather stop the program than let a wrong total reach a real customer. Take a calculation of your own from daily work, the kind still living in a notebook or a spreadsheet, and give it types before you give it a frontend.

Further reading

Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.

Originally published at https://practicaldevblog.blogspot.com/2026/12/gleam-shop-orders-tutorial.html

Top comments (0)