在Elm中使用Maybe和嵌套资源时出错

时间:2016-08-27 10:08:24

标签: elm

我正在构建我的第一个Elm SPA,我正在用不同的文件夹和模块组织我的组件。一切正常,直到我改变主模型:

type alias Model =
{ contacts : Contacts.Model.Model
, contact : Contact.Model.Model
, route : Routing.Route
}

对此:

type alias Model =
{ contacts : Contacts.Model.Model
, contact : Maybe Contact.Model.Model
, route : Routing.Route
}

我已经在代码库中进行了所有必要的更改,但是我遗漏了一些我找不到的东西,因为在主Update模块中我经常遇到这个编译错误:< / p>

The type annotation for `update` does not match its definition. - The type annotation is saying:

    Msg
    -> { ..., contact : Maybe Contact.Model.Model }
    -> ( { contact : Maybe Contact.Model.Model
    , contacts : Contacts.Model.Model
    , route : Routing.Route
    }
    , Cmd Msg
    )

But I am inferring that the definition has this type:

    Msg
    -> { ...
    , contact :
          { birth_date : String
          , email : String
          , first_name : String
          , gender : Int
          , headline : String
          , id : Int
          , last_name : String
          , location : String
          , phone_number : String
          , picture : String
          }
    }
    -> ( { contact : Maybe Contact.Model.Model
    , contacts : Contacts.Model.Model
    , route : Routing.Route
    }
    , Cmd Msg
    )

看起来我很想在某处传递Maybe Model,但我找不到它。这就是更新功能的样子:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
    ContactsMsg subMsg ->
        let
            ( updatedContacts, cmd ) =
                Contacts.Update.update subMsg model.contacts
        in
            ( { model | contacts = updatedContacts, contact = Nothing }, Cmd.map ContactsMsg cmd )

    ContactMsg subMsg ->
        let
            ( updatedContact, cmd ) =
                Contact.Update.update subMsg model.contact
        in
            ( { model | contact = updatedContact }, Cmd.map ContactMsg cmd )

这是完整的回购,错误为:https://github.com/bigardone/phoenix-and-elm/tree/feature/routes-refactoring/web/elm

我做错了什么?非常感谢你提前!

1 个答案:

答案 0 :(得分:2)

问题是由Update.update函数中的类型不匹配引起的。

update : Msg -> Model -> ( Model, Cmd Msg )

您必须在Update.elm中处理Maybe值:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        ContactsMsg subMsg ->
            let
                ( updatedContacts, cmd ) =
                    Contacts.Update.update subMsg model.contacts
            in
                ( { model | contacts = updatedContacts, contact = Nothing }
                , Cmd.map ContactsMsg cmd
                )

        ContactMsg subMsg ->
            case model.contact of
                Just contact ->
                    let
                        ( updatedContact, cmd ) =
                            Contact.Update.update subMsg contact
                    in
                        ( { model | contact = updatedContact }
                        , Cmd.map ContactMsg cmd
                        )

                Nothing ->
                    ( model, Cmd.none )