# Is it possible to expose type variants without importing into global namespace?

**URL:** <https://discourse.elm-lang.org/t/is-it-possible-to-expose-type-variants-without-importing-into-global-namespace/8747>\
**Category:** Learn\
**Created:** [November 7, 2022, 5:38pm UTC](https://discourse.elm-lang.org/t/is-it-possible-to-expose-type-variants-without-importing-into-global-namespace/8747 "2022-11-07T17:38:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HappMacDonald](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/happmacdonald/32/1069_2.png) [@HappMacDonald](https://discourse.elm-lang.org/u/HappMacDonald)\
**Post date:** [November 7, 2022, 5:38pm UTC](https://discourse.elm-lang.org/t/is-it-possible-to-expose-type-variants-without-importing-into-global-namespace/8747/1 "2022-11-07T17:38:57Z")

</div>

```auto
module Size exposing (Size, Maybe, ..etc..)

type Size = ...

type Maybe = Valid Size | Negative

----------------
module Main ..etc..
import Size exposing (Size)

case ... of
  Size.Negative -> ...
  Size.Valid measurement -> ...

```

Since this example doesn’t expose the constructors, we get “never heard of variant `Negative`”.

`import Size exposing (Size,Maybe(..))`

This exposes the constructors, and the above code works, but then elsewhere in the Main module:

```auto
type MaybeMessage = Maybe String

error: The `Maybe` type needs 0 arguments, but I see 1 instead

```

Which means it assumes I’m talking about Size.Maybe. But I basically _always_ explicitly name the module for anything exposed in it to keep my namespaces clean. So if I want Size.Maybe I’ll call it that, if I want Size.Negative constructor I’ll call that that as well.

Is it possible to expose import those constructors with_out_ dumping the Size.Maybe type into global namespace?

`import Size exposing (Size,Maybe(..) as Size.Maybe)`

for example does not work. :o

---

<div class="post-metadata">

**Author:** ![pit](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pit/32/5389_2.png) [@pit](https://discourse.elm-lang.org/u/pit)\
**Post date:** [November 7, 2022, 8:11pm UTC](https://discourse.elm-lang.org/t/is-it-possible-to-expose-type-variants-without-importing-into-global-namespace/8747/2 "2022-11-07T20:11:15Z")

</div>

You don’t have to expose the `Maybe` type in Main’s import. If you just write

```elm
import Size exposing (Size)

```

it should work.

---

<div class="post-metadata">

**Author:** ![hrnxm](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/hrnxm/32/5116_2.png) [@hrnxm](https://discourse.elm-lang.org/u/hrnxm)\
**Post date:** [November 8, 2022, 7:40am UTC](https://discourse.elm-lang.org/t/is-it-possible-to-expose-type-variants-without-importing-into-global-namespace/8747/3 "2022-11-08T07:40:38Z")

</div>

```auto
module Size exposing (Size, Maybe(..))

type Size = ...

type Maybe = Valid Size | Negative

-----------------------------------------------

module Main exposing (main)

import Size exposing (Size)

... _ = Size.Negative

```

This works for me (v0.19). Are you sure your module name is `Size` and not `Dir1.Dir2.Size`.  
Then it would be:

```auto
module Main exposing (main)

import Dir1.Dir2.Size as Size exposing (Size)

```

---

<div class="post-metadata">

**Author:** ![wolfadex](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/wolfadex/32/2461_2.png) [@wolfadex](https://discourse.elm-lang.org/u/wolfadex)\
**Post date:** [November 8, 2022, 12:51pm UTC](https://discourse.elm-lang.org/t/is-it-possible-to-expose-type-variants-without-importing-into-global-namespace/8747/4 "2022-11-08T12:51:34Z")

</div>

You can add `import Maybe` and have

```elm
type Maybe message = Maybe.Maybe String

```

I think that should work.

* * *

Separately, I think using the name `Maybe` for your module might be misleading to anyone who comes to read your code, including you, a year from now since it’s a core type. Instead of marking it as Valid, something like

```elm
parse : ? -> Result Error Size
parse input =
    -- parse to either a Size or an error

```

Not certain this fits with your code but generally I recommend trying to parse a value instead of separately mark something as valid/invalid. If you need to keep some type denoting whether or not it’s valid, then I’d go with something like

```elm
type Valid = Valid Size | Invalid

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [November 18, 2022, 12:51pm UTC](https://discourse.elm-lang.org/t/is-it-possible-to-expose-type-variants-without-importing-into-global-namespace/8747/5 "2022-11-18T12:51:50Z")

</div>

Closed on November 18, 2022, 12:51pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
