Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Union Types outside of declaring module in Elm

Tags:

elm

Given

--module 1
module Module1 exposing (Message) where

type Message
  = Test String
  | Error Int


--module 2
module Module2 exposing (sayTest, sayError) where

import Module1 exposing (Message)

sayTest : String -> Message
sayTest msg =
  Test msg  --error

sayError : Int -> Message
sayError code =
  Error code --error


processMessage : Message -> String
processMessage msg ->
  case msg of
    Test s -> s
    Error i -> toString i

How do I access Test and Error from module 2?

At the moment, I have to create functions in module 1 which when called will create the instances needed but as the list is getting longer, this is becoming impractical.

like image 420
ritcoder Avatar asked Jan 20 '16 22:01

ritcoder


1 Answers

You can expose all type constructors for an exported type like this:

module Module1 (Message (..)) where

Alternatively, if you wanted to export only a few type constructors, you can call them out individually:

module Module1 (Message (Test, Error)) where

type Message
  = Test String
  | Error Int
  | Foo String
  | Bar

In the above code, the Foo and Bar constructors remain private to the module.

like image 100
Chad Gilbert Avatar answered Sep 28 '22 01:09

Chad Gilbert