Published 13 Jan, 2021 by Louis Pilfold
It’s a new year and there’s a new Gleam release! Let’s take a look at what this one brings.
Gotta go fast
The compiler was no slouch but with this new version it’s faster than ever thanks to improvements to the Gleam and Erlang code generators, as well as a full rewrite of the parser by Greg.
With these changes compiling Gleam code is 1.8 times faster, and formatting Gleam code is a super nimble 4.6 times faster! On my machine Gleam compiling the standard library now takes ~170ms, and formatting all the files takes ~70ms.
This is only the start. Gleam compilation is currently single threaded and doesn’t make use of any caching, so there’s lots of opportunities to make big performance improvements in future.
Even better errors
Greg’s parser rewrite wasn’t just about performance, it also greatly improved the error messages caused by syntax errors. Here are some examples to click through:
Operator with a naked right side
Before
Now
Using ( ) as expression grouping
Before
Now
Trying to import a reserved word
Before
Now
Using a type name in place of a module
Before
Now
Extra separator anywhere
Before
Now
etc.Function in module constant type
Before
Now
Custom type with no constructors
Before
Now
Unterminated strings
Before
Now
Empty block
Before
Now
Opaque type alias
Before
Now
Type safe division
Division is a tricky problem thanks to the number zero. In mathematics the result of dividing any non-zero number by zero is undefined, making it is a question with no answer.
Sadly as programmers we can’t leave it undefined, have to do something. At some point some code will be written that divides by zero, so what should the language do when that happens? There are largely three options:
- Throw an exception
- Return
Infinity
- Return zero
Throwing an exception is the approach taken by Erlang and other BEAM languages, and historically this is the approach taken by Gleam. However, there is a problem.
Gleam aims to be an exception free language, and the assert
keyword is
intended as being the only way to crash a process. Errors should be
represented by the type system, so it is not in keeping with the design and
goals of the language for division to sometimes crash when dividing numbers.
Languages such as JavaScript that follow IEEE 754 return an Infinity
value
when dividing by zero, which may be positive or negative. This is a familiar
solution to many programmers, and it fits well with Gleam’s “never implicitly
crash” goal.
Unfortunately there is no Infinity
value on the Erlang virtual so Gleam
would have to implement this. While possible this would cause problems with
Erlang and Elixir interop- we could no longer safely pass Gleam numbers to
Erlang functions as they may be this special Infinity
value, which would
likely cause a crash. The same problem would occur when calling Gleam code
from Erlang or Elixir, resulting in adding Gleam libraries to your Erlang or
Elixir application being less appealling.
The last option is to follow in the footsteps of languages such as Pony, Isabelle, Lean and Coq and return zero. This might seem strange at first, but it is type safe, allows us to maintain good interop with other BEAM languages, and is representable on any other platform we might target in future.
In Gleam dividing by zero returns zero!
1337 / 0 // => 0
Improved documentation
Gleam has the ability to render HTML documentation for your code, ready to upload to Hexdocs.
Thanks to Michael Jones documentation for functions and types can automatically insert links to GitHub, GitLab, or BitBucket so you can quickly see how they are implemented.
Let us know if you use another source code hosts and would like support added too.
Following up from that Michał Łępicki added a version selector so that you can quickly find the documentation for the version of the library you are using. This dropdown is integrated with Hexdocs so it will display all versions of the library, rather than just earlier ones.
Other stuff
These are just some of the highlights, but there’s plenty more improvements made to the compiler and the standard library since the last release. For all the details check out the changelog files:
Discord chat
Since our creation of the Gleam Discord server the community has been more active than ever. It has been great to have so many members of the community there, building a helpful and friendly atmosphere. If you’d like to join click on the button below.
Try it out
If you want to try out the new version of Gleam head over to the getting started page. I’d love to hear how you find it and get your feedback so Gleam can continue to improve.
Want to view some existing Gleam projects? Head on over to the awesome-gleam list. Looking for something to build in Gleam? Check out the suggestions tracker.
And why not give us a star on GitHub? 😊
Supporting Gleam
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.
This release would not have been possible without the support of all the people who have sponsored and contributed to it, so a huge thank you to them.
- Adam Bowen
- Adam Mokan
- Al Dee
- Ali Farhadi
- Arian Daneshvar
- Arno Dirlam
- Ben Myles
- Bernardo Amorim
- bossek
- Bruno Michel
- Bryan Naegele
- Chris Young
- Christian Meunier
- clangley
- Clever Bunny LTD
- Cole Lawrence
- Connor Schembor
- Dan Mueller
- Dave Lage
- Dave Lucia
- David McKay
- David Pedersen
- Dennis Dang
- drew
- Drew Varner
- Edman Anjos
- Eric Meadows-Jönsson
- Erik Terpstra
- Florian Kraft
- Galaxy-Coding
- Greg Baraghimian
- Guilherme Pasqualino
- Hendrik Richter
- Herdy Handoko
- human154
- Ian González
- Ingmar Gagen
- Istvan
- Ivar Vong
- James MacAulay
- Jechol Lee
- Jérémy Pagé
- jiangplus
- Joe Corkerton
- John Palgut
- José Valim
- Jussi Norlund
- Lars Lillo Ulvestad
- Lars Wikman
- Leandro Cesquini Pereira
- Lucas Rosa
- Luna
- mario
- Mario Vellandi
- Mark Markaryan
- Markus
- Markus Hechenberger
- Matthew Cheely
- Matthew Cheely
- Max Nordlund
- Merlin Webster
- Michael Bausano
- Michael Jones
- Michał Łępicki
- Mike Lapping
- Mike Roach
- Milad
- Neil Lyons
- Nick Cernis
- Nick Reynolds
- Parker Selbert
- Patrick Ryan
- Pete Jodo
- Peter Saxton
- qingliangcn
- Quinn Wilton
- Raphael Megzari
- René Klačan
- rinsuki
- Robin Mattheussen
- Ryan Haskell-Glatz
- Sam Aaron
- Sasan Hezarkhani
- Sascha Wolf
- Saša Jurić
- Scott Wey
- Sean Jensen-Grey
- Sebastian Porto
- SEKUN
- Shane Sveller
- Shritesh Bhattarai
- Simone Vittori
- sjqtentacles
- Terje Bakken
- Tim Buchwaldt
- Tomasz Kowal
- Tomochika Hara
- Topher Hunt
- Tristan Sloughter
- Tyler Wilcock
- Wilson Silva
- Wojtek Mach
Thanks for reading! Have fun! 💜