Access Union Types outside of declaring module in Elm

情到浓时终转凉″ 提交于 2019-12-10 13:21:11

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/34912005/access-union-types-outside-of-declaring-module-in-elm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!