Tracks
/
Gleam
Gleam
/
Syllabus
/
Opaque Types
Op

Opaque Types in Gleam

4 exercises

About Opaque Types

Opaque types in Gleam are custom types where only the module that defines the type can construct or pattern match values of the type. This is useful for creating types that should only be used in a specific way.

Opaque types are defined using the opaque keyword.

pub opaque type PositiveInt {
  PositiveInt(inner: Int)
}

This PositiveInt type is to be used in situations where an int is wanted, but it has to be zero or greater. A regular Gleam Int could not be used as they can also be negative.

The module that defines this type can define a function to get the inner int value, and a function for creating the PositiveInt type which will return an error if the value is not positive.

pub fn from_int(i: Int) -> Result(PositiveInt, String) {
  case i {
    _ if i < 0 -> Error("Value must be positive")
    _ -> Ok(PositiveInt(i))
  }
}

pub fn to_int(i: PositiveInt) -> Int {
  i.inner
}

With this API other modules cannot construct a PositiveInt with a negative value, so any function that takes a PositiveInt can be sure that the value is positive.

Edit via GitHub The link opens in a new window or tab

Learn Opaque Types

Practicing is locked

Unlock 1 more exercise to practice Opaque Types