Otter Nonsense

Gleam v0.4 released!

Gleam has reached v0.4! This version has some new features, bug fixes, and some breaking changes. Let’s take a look.

Structs

Gleam’s map and tuple type has been removed and replaced with structs, which have many of the benefits of both.

Like maps structs are collections of named values, each with their own type. Unlike maps they are named, so two structs are not the same type just because they have the same fields by coincidence, making Gleam’s type system a little stricter by default.

Like tuples structs have constant access time, they do not need to be linearly scanned in order to find a field to access it.

Unlike either struct types are declared up-front by the struct keyword.

pub struct Cat {
  name: String
  cuteness: Int
}

Once declared the newly defined constructor can be used to create instances of the struct.

pub fn cats() {
  let cat1 = Cat(name: "Nubi", cuteness: 2001)
  let cat2 = Cat(cuteness: 1805, name: "Biffy")

  // Alternatively fields can be given without labels
  let cat3 = Cat("Ginny", 1950)

  [cat1, cat2, cat3]
}

One downside to structs is that they are less convenient for returning more than one value from a function than tuples were, as struct types need to be pre-declared. To negate this inconvenience the standard library declares Pair and Triple types in the gleam/pair and gleam/triple modules. If you wish to return more than 3 values from a function it is recommended to create a struct and name the values to make it more clear what they are.

Comparison operators

The next breaking change is to how the comparison operators >, >=, <, and <= work. Previously these operators would take any two values of the same type and determine which is larger according to Erlang’s ordering of values. This is convenience but may result in surprising behaviour when used with custom types such as enums.

For example, with the Order enum we would expect Gt (greater than) to be larger than Lt (less than), but according to Erlang’s value ordering this is not the case.

enum Order =
  | Gt
  | Eq
  | Lt
Gt > Lt  // => False

From this version on >, >=, <, and <= only accept Ints as arguments to avoid this surprising behaviour, and the >. >=. <. and <=. comparison operators have been added for comparing Floats.

For ordering other types comparison functions can be found in the standard library, such as order.compare and bool.compare.

Second class modules

Modules are no longer first class values in Gleam, meaning they can no longer be assigned to variables or used as arguments to functions.

First class modules were inspired by OCaml and intended to be a way to work with Erlang behaviours such as gen_stage from within Gleam. However after several months of using modules in Gleam it became clear that taking OCaml’s first class modules but not their functor module system resulted in Gleam’s modules being far less useful.

First class functions combined with Gleam’s other data structures is sufficient to use Erlang behaviours, while being easier to construct and compose, so first class modules were determined not to be the ideal solution here either.

With first class modules removed we no longer need a special syntax for accessing a field on a module, so the : operator has been removed and replaced with the . operator, making it the universal operator for accessing a named field on a module or container data type in Gleam.

import gleam/list

pub fn main() {
  list.reverse([1, 2, 3])
}

With the removal of modules and maps Gleam’s type system has become simpler, less structural and more nominal in style. This puts us in a good position to do research into new forms of polymorphism such as type classes or traits!

The rest

In addition to these features there’s been a number of other improvements to the quality of the error messages, the generated code, and a handful of bug fixes. 😊

If you want to try out the new version of Gleam head over to the installation page. I’d love to hear how you find it and get your feedback so Gleam can continue to improve.

Code BEAM lite Berlin

On the 11th of October 2019 I’ll be speaking at Code BEAM lite Berlin about Gleam. If you’re attending (or want to recommend to me good things to do on a weekend in Berlin) say Hello!

Thanks

Lastly, a huge thank you to the code contributors to this release!