我已经看到这个代码编译没有错误,我不能说是否是一个错误或如果它的预期.
type alias Foo = List
vs
type alias Foo = List String
Run Code Online (Sandbox Code Playgroud)
它不只是List.也允许自定义联合类型.例如:
type State value = Valid value | Invalid value
type alias Model1 =
{ someField : State String } -- i would say this is normal. State is a string..
type alias Model2 =
{ someField : State } -- this doesn't look right.
Run Code Online (Sandbox Code Playgroud)
并且还允许功能
function1 : List String -> Int
function1 aListOfStrings =
1
function2 : List -> Int
function2 whatisThisNow =
1
Run Code Online (Sandbox Code Playgroud)
但如果预计 - 如何推理呢?我无法绕过它.发挥它在这里.
第一个看起来还不错。定义type alias Foo = List应该允许您使用Foo而不是List. 但它无法编译(使用 Elm 0.18):
type alias Foo = List
names : Foo String --does not compile
names = ["a", "b"]
Run Code Online (Sandbox Code Playgroud)
看来类型别名在声明时没有经过充分检查,因此有可能创建根本无法使用的类型别名。
对于第一个示例,可以修复编译器以正确支持它。不过,第二个示例应该是编译时错误,因为无法获取类型List( 或State) 的值。Haskellers 会说List(or State) 有 kind * -> *,但运行时的值只能有 kind *。
我猜你在当前的 Elm 版本 (0.18) 中发现了一个错误
有趣的是,将上面的代码更改为
type alias Foo a = List a
names : Foo String
names = ["a", "b"]
-- compiles with Elm 0.18
Run Code Online (Sandbox Code Playgroud)
使其正常工作。但这两个代码片段应该是等效的。