Search code examples
gleam

Is it possible to import a record in an unqualified way?


Is it possible to import a record in an unqualified way? For example:

pub type Option(a) {
  Some(value: a)
  None
}

When I want to use a Some or None in another module, I always have to qualify them:

import option.{type Option}

let anOption: Option(Int) = option.Some(42)

This, however, does not work:

// This does not compile:
import option.{type Some} // error: The module `option` does not have a `Some` type
let some = Some(99)       // error: Unknown variable

Is there a way to import Some or None unqualifiedly?


Solution

  • You need to import the constructors to use them unqualified:

    import option.{type Option, None, Some}
    
    let anOption: Option(Int) = Some(42)
    

    Also, in case you're unaware, gleam's standard library already includes an option type.