Mok*_*oki 0 type-inference elm
我有一段Elm代码(getProjectView函数为简洁而省略):
type Model = Maybe List Project
model : Model
model = Nothing
getView : Model -> Html any
getView model =
case model of
Just projects ->
ul [] (List.map getProjectView projects)
Nothing -> p [] [ text "Still loading..." ]
Run Code Online (Sandbox Code Playgroud)
当我尝试编译以下代码段时,编译器失败并出现以下错误:
-- TYPE MISMATCH --------- E:\dev\irrelephant-code\client\elm\Views\Projects.elm
Tag `Maybe.Just` is causing problems in this pattern match.
32| Just projects ->
^^^^^^^^^^^^^
The pattern matches things of type:
Maybe a
But the values it will actually be trying to match are:
Model
Run Code Online (Sandbox Code Playgroud)
表明编译器不能推断出这Nothing是一个类型的值Model(它又是类型的别名Maybe List Project).
我在这做错了什么?有没有办法明确地将此Nothing标记为Model类型的值?
我使用的是elm v0.18.0
要定义模型作为type alias的Maybe (List Product).现在,使用type关键字,您将定义一个带有一个值的新union/tag类型Maybe,它需要类型List和的参数Product.
一个工作示例是https://ellie-app.com/3MhPcgGzXqRa1/0