Published 01 Mar, 2023 by Louis Pilfold
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.
Thanks
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!
- Aaron Gunderson
- Adam Brodzinski
- Adi Iyengar
- Alembic
- Alex Manning
- Alexander Koutmos
- Alexandre Del Vecchio
- Ali Farhadi
- Anthony Khong
- Anthony Scotti
- Arnaud Berthomier
- Arno Dirlam
- Ben Marx
- Ben Myles
- brettkolodny
- Brian Glusman
- Bruno Michel
- Bryan Paxton
- Carlos Saltos
- Charlie Duong
- chengshq
- Chew Choon Keat
- Chris Lloyd
- Chris Ohk
- Christopher Keele
- clangley
- Clay
- Cole Lawrence
- Colin
- Cristine Guadelupe
- Damir Vandic
- Dan Dresselhaus
- Daniel Tinsley
- Danny Martini
- Dave Lucia
- David Armstrong Lewis
- David Bernheisel
- David Flanagan
- Dennis Schröder
- dependabot[bot]
- Dmitry Matveyev
- Edon Gashi
- Elliott Pogue
- Enea Giger
- Erik Terpstra
- Fabian
- Filip Figiel
- Florian Kraft
- fly.io
- François Cardinaux
- Graeme Coupar
- Guilherme de Maio
- Gustavo Villa
- Harry Bairstow
- Hayleigh Thompson
- Henry Warren
- Hex
- human154
- Ian Gonzalez
- Ingmar Gagen
- inoas
- Ishat Gupta
- Ivar Vong
- James MacAulay
- Jan Skriver Sørensen
- Jen Stehlik
- jiangplus
- John Björk
- John Gallagher
- John Palgut
- Jonathan Arnett
- João Veiga
- Julien D
- Justin Blake
- Kapp Technology
- Kayla Washburn
- Kero van Gelder
- Kieran Gill
- Lars Wikman
- lidashuang
- Manuel Rubio
- Marcel Lanz
- Marcin Puc
- Marius Kalvø
- Mark Holmes
- Mark Markaryan
- Markus
- Markus Pettersson
- Martin Janiczek
- Mathias Jean Johansen
- matoruru
- Matt Van Horn
- matthew.cobbing
- matthew.cobbing
- Matthias Benkort
- Mauro Balbi
- Max Lee
- Maxim
- Michael Chris Lopez
- Michael Davis
- Michael Jones
- Michał Kowieski
- Michał Łępicki
- Michele Riva
- Mike Lapping
- Mike Roach
- Milton Mazzarri
- Nathaniel Knight
- Nick Reynolds
- Nicklas Sindlev Andersen
- NineFX
- Noah Betzen
- noam sauer-utley
- OldhamMade
- Ole Michaelis
- Oliver Searle-Barnes
- ontofractal
- paca94
- Parker Selbert
- Pascal Wengerter
- Paul Gideon Dann
- Pete Jodo
- porkbrain
- Praveen Perera
- qingliangcn
- Quinn Wilton
- Raúl Chouza
- Redmar Kerkhoff
- Rico Leuthold
- Robert Attard
- Robert Ellen
- Rodrigo Oler
- Ryan Winchester
- Sam Aaron
- Sascha Wolf
- Scott Wey
- Sean Jensen-Grey
- Sebastian Porto
- Seve Salazar
- shayan javani
- Signal Insights
- Simone Vittori
- Stefan Luptak
- Steven Joseph
- Strand Communications
- Szymon Wygnański
- Terje Bakken
- Theo Harris
- Tomasz Kowal
- Tristan de Cacqueray
- Tristan Sloughter
- Udo
- Uku Taht
- Wade Mealing
- Weizheng Liu
- Wilson Silva
- Wojtek Mach
- Worming
- YourMother-really
- Yu Matsuzawa
- zaatas
- Zsombor Gasparin
- Štefan Uram
Thanks for reading! Happy hacking! 💜