我有一段榆树代码(为了简洁而省略了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..." ]
当我尝试编译以下代码段时,编译器失败并出现以下错误:
-- 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
表示编译器无法推断出此Nothing
是Model
类型的值(而Maybe List Project
类型的别名)。
我在这里做错了什么?有没有办法明确地将此Nothing标记为Model类型的值?
我正在使用elm v0.18.0
答案 0 :(得分:7)
您希望将模型定义为type alias
Maybe (List Product)
。现在,使用type
关键字,您将定义一个新的union / tag类型,其中包含一个值Maybe
,它需要类型为List
和Product
的参数。