Elm没有正确地推断出Nothing值的类型

时间:2017-07-17 20:39:49

标签: type-inference 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..." ]

当我尝试编译以下代码段时,编译器失败并出现以下错误:

-- 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

表示编译器无法推断出此NothingModel类型的值(而Maybe List Project类型的别名)。

我在这里做错了什么?有没有办法明确地将此Nothing标记为Model类型的值?

我正在使用elm v0.18.0

1 个答案:

答案 0 :(得分:7)

您希望将模型定义为type alias Maybe (List Product)。现在,使用type关键字,您将定义一个新的union / tag类型,其中包含一个值Maybe,它需要类型为ListProduct的参数。

一个工作示例是https://ellie-app.com/3MhPcgGzXqRa1/0