Gleam is a type safe and scalable language for the Erlang virtual machine and JavaScript runtimes. Today Gleam v0.27.0 has been released, let’s take a look at what’s new.

Using patterns in use expressions

The Gleam v0.25 release introduced the use expression, a feature that helps avoid “callback hell” in Gleam programs. It is quite powerful and can be used to emulate many features found in other languages such as early returns, list comprehensions, async/await, monadic do syntax, and more!

pub fn handle(req: Request) -> Response {
  // Return an error for non-post requests
  use <- when(req.method != Post, return: method_not_allowed)

  // Parse the request body or return an error
  use json <- require_json_body(req)

  // Open a database connection, closing it when we're done
  use conn <- database.with_connection

  case database.insert(conn, json) {
    Ok(record) -> created_response(record)
    Error(err) -> bad_request_response(err)

One limitation of the use expression was that patterns could not be used with their assignments, only bare variables. This means you could not destructure a tuple or other data structure with them.

As of this release patterns are now supported in the use expression, for all your destructuring needs!

use #(first, second) <- some_tuple_function()

Don’t Panic!

This release introduces the panic keyword, a simple little keyword that causes the program to crash. Why would you want to do this?

Your program may not have sufficiently constrainted types to make invalid state unrepresentable. In this case you may want to use panic to crash the program when an invalid state is reached, which is often preferable than silently continuing or programming defensively.

Alternatively you may be embracing the Erlang philosophy of “let it crash” and choosing to use handle unexpected exceptional errors with an Erlang style supervision tree. This strategy is particularly effective when rapidly prototyping or in situations where there is no way to reasonably recover from an error, or client to show an error message to.

case this_should_never_fail() {
  Ok(value) -> continue(value)

  // Oh no! Something went horribly wrong!
  Error(_) -> panic

Panic is similar to the assert keyword, only it always crashes rather than conditionally. Speaking of the assert keyword…

Towards a better assert

Gleam’s assert keyword is used to ensure that data matches a given pattern, crashing the program if it does not. This is useful, but using assert for this functionality means that we can’t have an assert feature that works with boolean expressions, as is commonly found in other languages.

To remedy this the pattern matching feature has been moved to this let assert syntax. The existing assert keyword has been deprecated and will later be used for a more conventional assertion feature, to be part of a larger Gleam milestone based around improving the ergonomics of testing Gleam code.

let assert Ok(value) = this_should_never_fail()

Deprecations are annoying, and nobody likes having to fix their code, so the Gleam tooling will automatically upgrade your syntax when you run gleam format or format the code in your editor.

Moving beyond try

try expressions were introduced way back in v0.9 as a way of avoiding callback hell when working with functions that can either return a value or an error. Now that we have use expressions, the less general try expressions are redundant. We always prefer to have fewer ways to do the same thing in Gleam, and fewer things to have to learn, so try expressions are deprecated in this release.

A try expression can be replaced by a use expression with the then function from the gleam/result module. Once try is no longer a keyword the then function will be aliased to try.

// With try
try file = open_file()

// With use
use file <- then(open_file())

// After `try` is removed
use file <- try(open_file())

This is still annoying to fix by hand, so the new gleam fix command has been created to automatically upgrade your code for you.

And that’s it! There’s also lots of other small improvements and bug fixes, so check the full release notes for more details.


Gleam is made possible by the support of all the kind people and companies who have very generously sponsored or contributed to the project. Thank you all!

If you like Gleam consider sponsoring or asking your employer to sponsor Gleam development. I work full time on Gleam and your kind sponsorship is how I pay my bills!

Thanks for reading! Happy hacking! 💜