Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing and importing a data constructor from another module

Tags:

elm

In my Elm program I want to define a type in one module:

MyModule.elm:

module MyModule exposing (MyType)
type MyType = Constr1 String | Constr2 Int

and construct a value of this type in another module:

Main.elm:

import MyModule exposing (MyType)
import Html exposing (text)
main =
    let x = Constr1 "foo" in
        text "hello"

When I build this with:

elm-package install elm-lang/html && elm-make Main.elm

I get:

NAMING ERROR ------------------------------------------------------- Main.elm

Cannot find variable `Constr1`

6|     let x = Constr1 "foo" in
               ^^^^^^^


Detected errors in 1 module.     

If I use (..) in both exposing clauses, this compiles fine, but I would like to know how to express that I want to expose the constructors.

Side note: I would also like to know where I should have found this in the docs.

like image 497
Andy Balaam Avatar asked May 28 '26 22:05

Andy Balaam


1 Answers

You can specify which constructors to expose like this:

module MyModule exposing (MyType(Constr1, Constr2))

All constructors of a type can be exposed using (..) notation:

module MyModule exposing (MyType(..))

And if you don't want to expose any constructors (meaning you have other exposed functions that create values of your type, you specify only the type:

module MyModule exposing (MyType, otherFunctions)

There is community documentation around this topic at elm-community.github.io/elm-faq

like image 146
Chad Gilbert Avatar answered Jun 02 '26 21:06

Chad Gilbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!