(updated ) by

OR Type in Gleam

I am diving deeper into gleam with making an interface for an Erlang sqlite library and is common to have a OR type.

:ok | {:error, error}

After reading gleam.run/…/custom-types.html#erlang-interop, which suggests blank constructor(?) types are atoms in Erlang.

pub type OkOrError {
   Ok
   Error(String)
}

Which results in the Gleam to Erlang version:

-type ok_or_error() :: ok | {error, binary()}.

The error can be a blank type in the Erlang libraries so is suggested to use the Dynamic type. Then convert the dynamic type into the proper type to handle the type differences appropriately.

-type ok_or_error() :: ok | {error, term()}.
pub type OkOrError {
   Ok
   Error(Dynamic)
}

See details about Dynamic and how to convert from dynamic to a gleam type.