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

Elm doesn't deduce type of Nothing value properly

我有一段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

表明编译器无法推断出此 Nothing 是类型 Model 的值(这又是类型 Maybe List Project 的别名)。

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

我正在使用 elm v0.18.0

您想要将模型定义为 Maybe (List Product)type alias。现在,使用 type 关键字,您正在定义一个具有一个值 Maybe 的新 union/tag 类型,它期望参数类型为 ListProduct.

一个有效的例子是https://ellie-app.com/3MhPcgGzXqRa1/0