代码之家  ›  专栏  ›  技术社区  ›  MarengoHue

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

  •  0
  • MarengoHue  · 技术社区  · 7 年前

    我有一段Elm代码(对于brievity,省略了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 ).

    我正在使用elm v0.18.0

    1 回复  |  直到 7 年前
        1
  •  7
  •   bdukes Jon Skeet    7 年前

    type alias 属于 Maybe (List Product) type 关键字,您正在使用一个值定义新的联合/标记类型, Maybe ,预期为参数,类型为 List Product

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