Published 19 Jun, 2022 by Louis Pilfold
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.
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!
- 0xOler ▲
- Aaron Ross
- Adam Bowen
- Adam Brodzinski
- Adi Iyengar
- Alex Manning
- Alexander Koutmos
- Ali Farhadi
- Anthony Scotti
- Arnaud Berthomier
- Arno Dirlam
- Beam it Recruitment
- Ben Marx
- Ben Myles
- Bernat Jufré Martínez
- Brian Glusman
- Bruno Michel
- Carlos Saltos
- Charlie Duong
- Chew Choon Keat
- Chris Lloyd
- Chris Young
- Christian Meunier
- Christopher Keele
- clangley
- Clay
- Cole Lawrence
- Colin
- Consoli
- Copple
- Cristine Guadelupe
- Damir Vandic
- Dan Dresselhaus
- Dan Mueller
- Dashuang Li
- Dave Lucia
- David Armstrong Lewis
- David Bernheisel
- David Flanagan
- Dennis Tel
- dependabot[bot]
- Dusty Phillips
- E Jikan
- Edon Gashi
- Elliott Pogue
- Eric Meadows-Jönsson
- Erik Terpstra
- Florian Kraft
- fly.io
- Graeme Coupar
- Guilherme de Maio
- Gustavo Villa
- Harry Bairstow
- Hayleigh Thompson
- Henry Warren
- Herdy Handoko
- Herminio Torres
- human154
- Ian González
- Ingmar Gagen
- inoas
- Ivar Vong
- James MacAulay
- Jan Skriver Sørensen
- Jechol Lee
- jiangplus
- Joe Corkerton
- John Doneth
- John Gallagher
- John Palgut
- Jonathan Arnett
- Josh Price
- josh rotenberg
- José Valim
- Josías Alvarado
- João Veiga
- Julien D
- Justin Blake
- Kapp Technology
- Kieran Gill
- Lars Lillo Ulvestad
- Lars Wikman
- Marcel Lanz
- Marcin Puc
- Marius Kalvø
- Mark Holmes
- Mark Markaryan
- Markus
- Martin Janiczek
- Mathias Jean Johansen
- Matt Van Horn
- McKayla Washburn
- Michael Chris Lopez
- Michael Davis
- Michael Jones
- Michał Kowieski
- Michał Łępicki
- Michele Riva
- Miguel Serrano
- Mike Lapping
- Mike Roach
- Milton Mazzarri
- moogle
- mrkutly
- Nathaniel Knight
- Nick Reynolds
- NicklasXYZ
- NineFX
- OldhamMade
- Ole Michaelis
- Oliver Searle-Barnes
- Parker Selbert
- Patrick Thomson
- Pete Jodo
- Peter
- porkbrain
- Praveen Perera
- qingliangcn
- Quinn Wilton
- Raúl Chouza
- Redmar Kerkhoff
- Reio Piller
- Reto Kramer
- Robert Attard
- Ryan Winchester
- Sam Aaron
- Santi Lertsumran
- Sascha Wolf
- Saša Jurić
- Scott Wey
- Sean Jensen-Grey
- Sebastian Porto
- Sebastian Ziemann
- Shane Sveller
- Shunji Lin
- Shuqian Hon
- Simone Vittori
- SmartLogic
- Strand Communications
- Terje Bakken
- Tobias Hermann
- Tomasz Kowal
- Tomochika Hara
- Tristan de Cacqueray
- Tristan Sloughter
- tynanbe
- Uku Taht
- Vincent Le Goff
- WeiZheng Liu
- Wilson Silva
- Wojtek Mach
- YourMother-really
- Yu Matsuzawa
- Štefan Ľupták
Thanks for reading! Happy hacking! 💜