Elm 从另一个模块公开和导入数据构造函数
在我的Elm程序中,我想在一个模块中定义一个类型: MyModule.elm:Elm 从另一个模块公开和导入数据构造函数,elm,Elm,在我的Elm程序中,我想在一个模块中定义一个类型: MyModule.elm: module MyModule exposing (MyType) type MyType = Constr1 String | Constr2 Int 并在另一个模块中构造此类型的值: Main.elm: import MyModule exposing (MyType) import Html exposing (text) main = let x = Constr1 "foo" in
module MyModule exposing (MyType)
type MyType = Constr1 String | Constr2 Int
并在另一个模块中构造此类型的值:
Main.elm:
import MyModule exposing (MyType)
import Html exposing (text)
main =
let x = Constr1 "foo" in
text "hello"
当我使用以下工具构建此应用程序时:
elm-package install elm-lang/html && elm-make Main.elm
我得到:
NAMING ERROR ------------------------------------------------------- Main.elm
Cannot find variable `Constr1`
6| let x = Constr1 "foo" in
^^^^^^^
Detected errors in 1 module.
如果我在两个exposing
子句中都使用(..)
,这可以很好地编译,但是我想知道如何表示我想要公开构造函数
旁注:我还想知道我应该在文档中的何处找到它。您可以指定要公开的构造函数,如下所示:
模块MyModule公开(MyType(Constr1,Constr2))
一个类型的所有构造函数都可以使用(..)
符号公开:
模块MyModule暴露(MyType(..)
如果您不想公开任何构造函数(这意味着您有其他公开的函数来创建您的类型的值,那么您只需指定类型:
modulemymodule公开(MyType,otherFunctions)
在上有关于此主题的社区文档,我会将其添加到社区文档中,以使其更加精彩,谢谢。我会去阅读我不应该这样做的原因。