importcompiler-errorsgleam

How to import only type constructor in Gleam?


I want to import a type constructor, but not a type itself.

For example, I need to use a None from an Option type:

import gleam/option.{type None}

pub type MyType {
  MyType(value: Option(Int))
}

pub fn build_type() {
  MyType(None)
}

However, this code doesn't compile:

error: Unknown module type
  ┌─ /src/main.gleam:1:22
  │
1 │ import gleam/option.{type None}
  │                      ^^^^^^^^^ Did you mean `Option`?

The module `gleam/option` does not have a `None` type.

How can I correctly import the None type constructor?


Solution

  • In Gleam, None is not a type, but a type constructor. Type constructors don't require a type keyword in the import statement. Instead, you can directly import it as follows:

    import gleam/option.{None}