Mark Karavan
Mark Karavan

Reputation: 2684

Module exposing in Elm 0.18

I have an elm module of Types that consists of types and type aliases Foo, Bar and Baz. When I export everything like so, the code works:

module Types exposing (..)

However, if I include all of the types explicitly, the code breaks.

module Types exposing (Foo, Bar, Baz)

This is also true of the import statements on the consuming file; both need to be exposing (..)

What is the difference between these two approaches?

Upvotes: 6

Views: 370

Answers (2)

jpierson
jpierson

Reputation: 17384

I'm only learning Elm myself very recently but it sounds like since one of your types (Msg) is being defined as a union type that you will need to use the Msg(..) syntax when exporting this type.

module Types exposing (Msg (..))

type Msg = FirstAction | SecondAction | ThirdAction

Or in the case of importing the union type.

import Types exposing (Msg (..))

Please see GitHub issue #968 for an explanation of why this is required for union types.

Upvotes: 8

Arthur Welf
Arthur Welf

Reputation: 176

Look at this type definition:

    type Foo
        = Foo
        | Baz
        | Quux

You see two Foo's: the previous is type constructor and the latter is data constructor. Type constructors and data constructors live in different namespaces, so you can use the same name for both.

As you write

    import Types exposing (Foo)

you are importing type constructor Foo only. To import Foo's data constructors, you have to do it explicitly in parentheses after the import of type constructor:

    import Types exposing (Foo (Foo, Baz))

or, if you want to import all data constructors of this type:

    import Types exposing (Foo (..))

Upvotes: 3

Related Questions