在我的Elm程序中,我想在一个模块中定义一个类型:
MyModule.elm:
module MyModule exposing (MyType)
type MyType = Constr1 String | Constr2 Int
Run Code Online (Sandbox Code Playgroud)
并在另一个模块中构造此类型的值:
Main.elm:
import MyModule exposing (MyType)
import Html exposing (text)
main =
let x = Constr1 "foo" in
text "hello"
Run Code Online (Sandbox Code Playgroud)
当我构建这个:
elm-package install elm-lang/html && elm-make Main.elm
Run Code Online (Sandbox Code Playgroud)
我明白了:
NAMING ERROR ------------------------------------------------------- Main.elm
Cannot find variable `Constr1`
6| let x = Constr1 "foo" in
^^^^^^^
Detected errors in 1 module.
Run Code Online (Sandbox Code Playgroud)
如果我(..)在两个exposing子句中使用,这编译很好,但我想知道如何表达我想暴露构造函数.
旁注:我也想知道我应该在文档中找到这个.
您可以指定要公开的构造函数,如下所示:
module MyModule exposing (MyType(Constr1, Constr2))
Run Code Online (Sandbox Code Playgroud)
可以使用(..)符号公开所有类型的构造函数:
module MyModule exposing (MyType(..))
Run Code Online (Sandbox Code Playgroud)
如果您不想公开任何构造函数(意味着您有其他创建类型值的公开函数),则只指定类型:
module MyModule exposing (MyType, otherFunctions)
Run Code Online (Sandbox Code Playgroud)
有关此主题的社区文档,请访问elm-community.github.io/elm-faq