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 , Void and Never. The Maybe type represents a nillable value.

Promise

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

In Mint promises have two parameters Promise(error, result) :

  • error - the type of the error
  • result - the type of the result

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

Promise(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.

Promises are used in a sequence and parallel expressions.

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 */

Results are used in sequence , parallel and try expressions.

Void

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

Void can only be explicitly returned with the void keyword.

Never

The never type is used to describe tasks (promises or results) that can never fail. For example, here is the type of task that can never fail: Promise(Never, String)

These tasks don't need to be caught in sequence , parallel and try expressions.

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)
}