First Impressions of Gleam

With the announcement that this year’s Advent of Code is going to be shorter, I’m considering participating for the first time in a few years, and I’m in the mood to try a new language. Exploring what’s out there, I found one: Gleam.

Gleam: A Small Language

Gleam is a small, statically typed functional language in the ML family.

// Sum positive even numbers; skip odds; fail on negatives.
pub fn sum_evens(numbers: List(Int)) -> Result(Int, String) {
  numbers
  |> list.try_fold(0, fn(sum, n) {
    case n {
      n if n < 0 -> Error("no negatives")
      n if n % 2 == 0 -> Ok(sum + n)
      _ -> Ok(sum)
    }
  })
}

It made headlines earlier this year when, the year after its first stable release, Gleam achieved second-most-desired language status in the Stack Overflow Developer survey (coming in just behind Rust).

It’s a small language in the sense that it has a small surface area, with less to learn, and fewer ways to accomplish a given task. For more on the design philosophy, see the 1.0 release announcement.

Gleam has two compilation targets: JavaScript and Erlang.

Getting Started

Gleam installed nicely with mise, which has quickly become my preferred way to manage project-specific development tools:

mise use aqua:gleam-lang/gleam
mise use erlang

Once installed, the gleam CLI provides build and run like you’d expect, but also a package manager and niceties like a code formatter, test runner, and documentation generator. And it can generate you a new project with gleam new hello_world.

Gleam’s language server works with a bunch of editors; I started with familiar VS Code, then tried out Zed, and both worked well, with squiggles, tooltips, and autoformatting.

First Impressions

After spending a few hours with Gleam, here are some observations in no particular order:

  • From one codebase, you can use either runtime (-t javascript vs -t erlang). While I’m sure there are subtle differences between the two, I haven’t encountered any yet; the abstraction held, and everything worked both ways.
  • You can really feel the language’s small surface area when you walk through the Language Tour. Their claim that you could learn it in an afternoon seems reasonable to me.
  • That language tour? It runs the compiler in your browser, and it’s quick. Build errors and program output appear about as fast as you can type.
  • You can also feel the small surface area when you reach for if statements or loops: the language has neither! (Gleam is clearly very opinionated, but its opinions mostly happen to align with mine :)
  • Another thing Gleam doesn’t have is mutation. Contrast this with Rust or F#, which default to immutable but offer a special keyword as an escape hatch. Gleam burned the ships, and there’s no going back.
  • The default test framework (“gleeunit“) is Spartan but adequate.
  • Some of the compiler errors (also visible via editor tooltips) are particularly friendly, picking up what you were trying to do and taking the opportunity to teach a concept. Here are a couple I hit early on:

What’s Next

To warm up, I solved a few puzzles from Cassidoo’s newsletter. For more practice, Exercism has a large Gleam track, and previous years of Advent of Code are still available

Check out awesome-gleam to see what the ecosystem is up to.

Have you used Gleam? Are you planning to participate in this year’s Advent of Code, with this or another language? Let me know in the comments.

Conversation

Join the conversation

Your email address will not be published. Required fields are marked *