Try Install Learn Blog API Packages GitHub
Pages

Built-in Types

Mint comes with several built-in types. These are used in control expressions: Promise , Result and Void. The Maybe type represents a nillable value.

Promise

The promise type represents a JavaScript promise - an asynchronous computational task.

In Mint promises have only one parameter result - which is the type the result of the computation.

A good example is a HTTP request which in Mint looks like this:

Promise(Result(Http.ErrorResponse, Http.Response))

where Http.ErrorResponse is a record containing information about the error that happened while Http.Response is a record containing the response of the request.

Result

The result type, represents a synchronous task that might fail. It is defined as an enum:

enum Result(error, value) {
  Err(error)
  Ok(value)
}

In Mint, results have two parameters Result(error, value):

  • error - the type of the error
  • value - the type of the value in the Ok case

For example, converting a String to a Number:

  • If the conversion fails we get an error:
    "blah"
    |> Number.fromString()
    |> Maybe.toResult("Conversion failed.")
    |> Result.isError() /* true */
  • If the conversion succeeds we get a value
    "10"
    |> Number.fromString()
    |> Maybe.toResult("Conversion failed.")
    |> Result.isOk() /* true */

Void

The void type represents an expression which cannot have any value.

Void can only be explicitly returned with the void keyword.

Maybe

The maybe type represents a value which may or may not exist. It's defined as an enum:

enum Maybe(value) {
  Just(value)
  Nothing()
}

For example here is a user who may or may not have a car:

record Car {
  type : String,
  engine : String
}

record User {
  name : String,
  car : Maybe(Car)
}