在Elm中的声明模块之外访问联合类型 [英] Access Union Types outside of declaring module in Elm

查看:68
本文介绍了在Elm中的声明模块之外访问联合类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出

--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

如何访问 Test 模块2中的错误

目前,我必须在模块1中创建函数,该函数在调用时将创建

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.

推荐答案

您可以公开导出的所有类型构造函数像这样输入:

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

在上面的代码中,Foo和Bar构造函数对模块保持私有。

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

这篇关于在Elm中的声明模块之外访问联合类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆