elm-lang.org
elm-lang.org copied to clipboard
Exporting Union Types in Elm Syntax
If we have a submodule with:
type Animal = Cat | Dog
we can export Cat and Dog with:
module Submodule (Animal(..)) where
and import with
import Submodule exposing (Animal(..))
Can we add something that explains this on the syntax page
It is mentioned here Elm Syntax — Modules already, but I agree, it is not explicit. The best I could think of is adding a commentary about the presence of open imports for Union Types in Modules section.
I think it should be mentioned here too: https://guide.elm-lang.org/types/union_types.html
Yeah, this is mysterious to a beginner and not emphasized enough in the docs (or at least not enough that I remembered it). This github issue saved me :-)
Glad I found this github issue, because I couldn't deduct this from Elm Syntax - Modules
Puh I am so happy to have seen this issue... Now all my Types go into a single Types.elm
This was a good 30 minutes of headache for me... glad this issue is the first result when you google it.
For anyone looking here. Export your union type with module Module exposing (UnionType(..), otherthing, etc)
and import it with import Module exposing (UnionType(..))
.
For anyone looking here. Export your union type with
module Module exposing (UnionType(..), otherthing, etc)
and import it withimport Module exposing (UnionType(..))
.
I somehow knew that importing it with (..) was possible, but I didnt think of exporting for some reason. ¯_(ツ)_/¯
This means that you can't access Dog
and Cat
without explicitly exposing them right? Just import Submodule
isn't enough to access them it seems like.