Gleam v0.8 released!

It’s been a really good couple months for Gleam. The community has been growing, we’ve been getting new features and improvements, and now it’s time for another release. Let’s take a look!

HTML Documentation

Documentation has been a bit of sore point up until now. If you wanted to write a program in Gleam there was no other way to see what types functions were available to use. More than likely you would have to have the source code for the standard library open in a window somewhere, which isn’t the nicest way to learn a language.

To solve this problem Gleam can now generate HTML documentation from your Gleam source code and publish to HexDocs, the home of documentation in the Erlang ecosystem.

Check out an example of the new documentation here.

Gleam format

Consistency matters in code! Having a consistent style makes code easier to read and easier to work with. On the other hand, maintaining a consistent style by hand is an expensive time sink, and it’s all too easy to waste time debating exactly where is the right place to put those curly braces in code reviews.

The solution to these problems is a code formatter. Write whatever code you want without spending any time on style, press save, and have your editor automatically fix the style before you’ve taken your fingers off the keyboard.

Starting with this release Gleam includes a formatter, and all new Gleam projects are preconfigured to have their format validated on CI. Enable the formatter in your editor and never waste time on fixing style again.

If you’re interested in learning more about formatters or how they work, a few years back I gave a talk on the subject. It is in the context of the Elixir language, but it all holds true to Gleam as well.

Fancy pipes

The pipe operator is a favourite feature of Gleam, it allows us to write chains of functions that read top-to-bottom and left-to-right.

It works by taking the value on the left hand side and using it as the argument to the function on the right hand side.

// Without pipe
print(eval(read(stdin)), stdout)

// With pipe
stdin |> read |> eval |> print(_, stdout)

In the example above the function “print” takes two arguments, so the function _ capture syntax has been used to tell Gleam where to insert the left hand side value. In practice most Gleam functions take multiple arguments, so the capture syntax is needed more often than not.

scores
|> list.map(_, double)
|> list.fold(_, 0, add)
|> should.equal(_, 42)

With Gleam v0.8’s fancy pipe this boilerplate is no longer needed!

When used with a function call on the right hand side (a |> b(1, 2)) the pipe will first check to see if the left hand value could be used as the first argument to the call (b(a, 1, 2)), and if not it falls back to calling the result of the right hand side as a function (b(1, 2)(a)).

scores
|> list.map(double)
|> list.fold(0, add)
|> should.equal(42)

Much better! And what’s more this is resolved at compile time using the type system so there is no performance or safety penalty to use this over nested function calls.

Accessor syntax

Pattern matching is great, but sometimes we just want to get a single value out of a record or tuple. To enable this Gleam now supports the .field syntax that’s common in other languages.

It works with records:

let student = Student(subject: "Theatre")
let mary = Person(name: "Mary", role: student)

mary.name         // => "Mary"
mary.role.subject // => "Theatre"

And it works with tuples:

let pair = tuple("Apples", "Oranges")

pair.0 // => "Apples"
pair.1 // => "Oranges"

Spread operator

Gleam’s pattern matching has been expanded with a new operator called the spread operator, which allows you to write a partial pattern for a record.

// Without spread
let Uri(
  scheme: scheme,
  port: port,
  path: _,
  query: _,
  fragment: _,
  authority: _,
  userinfo: _,
  host: _,
) = uri

// With spread
let Uri(scheme: scheme, port: port, ..) = uri

To be consistent with records the syntax for prepending to a list and for matching on the head of a list have been updated to also use the spread operator.

let new_list = [1, 2, 3, ..existing_list]

case list {
  [first, ..] -> first
  [] -> 0
}

The old [x | xs] list syntax is deprecated but is still accepted. If you run the formatter it will automatically upgrade your code for you, so do that first!

The rest

In addition to these changes there’s also support for numbers and numerical operators in case guards, an “assert” syntax for inexhaustive pattern matching, a “todo” syntax for type checking functions without a finished implementation, a handful of performance improvements, and a whole bunch of bug fixes!

Outside of the compiler we continue to grow the standard library, and we have made good progress in OTP support with a prototype for type safe supervision trees.

For more information check out the changelog on Github.

Thanks

The most exciting thing for me about this release is just how much the community has grown since the last release. We’ve more people writing Gleam, more people sharing ideas, and more people working on the compiler. We’ve even got the beginnings of a Gleam web server and web framework in the Midas project!

It would not have been possible to have a release this big without the support of all Gleam’s sponsors and contributors, so a huge thank you to them:

If you would like to help make strongly typed programming on the Erlang virtual machine a production-ready reality please consider sponsoring Gleam via the GitHub Sponsors program.

Thanks for reading! Have fun! 💜