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

TypeScript declarations

Excellent Interop with other languages on the target runtimes is a core concern of Gleam. We want to take full advantage of the existing ecosystems, and we also want to give back to those ecosystems and make Gleam code easy to use from them.

To aid with interop when running on JavaScript runtimes Gleam can now generate TypeScript declaration files, enabling TypeScript users to have full type checking when calling code written in Gleam. These definition files will include accurate definitions for all the types, functions, and constants defined in Gleam.

This feature can be enabled in your gleam.toml.

name = "my_project"
version = "1.0.0"

[javascript]
typescript_declarations = true

With this a .d.ts file will be generated for each Gleam module compiled. For example this Gleam module:

pub const answer = 42

pub fn map(result, fun) {
  case result {
    Ok(a) -> Ok(fun(b))
    Error(e) -> Error(e)
  }
}

Will produce this .d.ts file.

import * as _ from "./gleam.d.ts";

export const answer: number;

export function map<A, E, B>(
  result: _.Result<A, E>,
  fun: (x: A) => B,
) => _.Result<B, E>

Note that while the Gleam code here doesn’t have any type annotations the TypeScript declaration file still has all the type information. Gleam’s type inference doesn’t require annotations, Gleam code is always fully type checked.

Thank you to E Jikan of VStream for this feature!

Multi-variant accessors

Gleam has the .field_name syntax found in many languages. With it you can access values within a custom type by their name.

pub type SchoolPerson {
  Teacher(name: String, subject: String)
}

pub fn get_name(person: SchoolPerson) {
  person.name // Using the accessor syntax
}

However this was only supported for custom types with a single variant. If a Student variant was added to the above then the accessor syntax would not be usable.

pub type SchoolPerson {
  Teacher(name: String, subject: String)
  Student(name: String, age: Int) // A new variant
}

With this release the accessor syntax can be used for fields found in all variants of a custom type. In this specific example .name can be used, but .subject and .age cannot as they are defined only in a single variant.

Thank you to Harry Bairstow for this feature!

Deployment artefacts

People are starting to run web applications written in Gleam in production. This is very exciting! To help with this we are focusing on deployment tooling and documentation, first with the new gleam export command.

By running gleam export erlang-shipment the compiler will produce a directory of Erlang bytecode and configuration that can be copied onto a server or into a container and then run with ./entrypoint.sh run. Gleam does not need to be installed on the production server for this to be used, only the virtual machine is required.

Automatable package publishing

Gleam integrates with Hex, the package repository for the BEAM ecosystem. From this release commands that work with Hex can take authentication credentials from the HEXPM_USER and HEXPM_PASS environment variables, and auto-accept confirmation prompts with the --yes flag. This makes it possible to do things such as automatically publish Gleam packages on a CI system or from a build script.

Thank you to Robert Attard for this feature!

And on a related note, recently published packages can be replaced with gleam publish --replace. This is useful for correcting any mistakes made during the process of releasing a package. Thank you Shayan Javani!

Better debugging

This release brings the new string.inspect function, which will convert any value to a string in Gleam syntax.

string.inspect([1, 2, 3]) // "[1, 2, 3]"
string.inspect(Ok("Hi!")) // "Ok(\"Hi!\")"
string.inspect(Nil)       // "Nil"

The io.debug function has also been upgraded to use this function, so now debug printing values will result in Gleam syntax being used rather than the runtime Erlang representation.

Thank you to inoas for this feature!

How can I try it?

Instructions on how to install the latest version of Gleam can be found on the getting started page of the website. Once installed check out the language tour for an introduction to the language.

For all the details of this release check out the changelog.

Supporting Gleam

If you would like to support me in making Gleam please consider sponsoring Gleam or asking your employer to sponsor Gleam. Every donation makes a difference, no matter how small, so thank you for your help.

⭐ Or alternatively give us a star on GitHub! ⭐

And thank you!

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

Thanks for reading! Happy hacking! 💜