user3282666
user3282666

Reputation: 670

How to upate a record that has another record within it?

I have a record :

type alias Point = {x : Int, y : Int}

I have another record like this :

type alias SuperPoint = {p : Point, z : Int}

p = Point 5 10
sp = SuperPoint p 15

Now if I need to update SuperPoint.z I can do this :

{sp | z = 20}

How do I update SuperPoint.Point?

Upvotes: 1

Views: 82

Answers (2)

rofrol
rofrol

Reputation: 15226

Right now you have four ways to update:

  • Two of them you can find below in code
  • third is using Monocle.
  • fourth: Re-structure your model with Dictionaries and handle the update in a proper generic way
  • A the end there is also a code which doesn't work, but could be useful, if that worked that way

Example in elm-0.18

import Html exposing (..)


model =
    { left = { x = 1 }
    }


updatedModel =
    let
        left =
            model.left

        newLeft =
            { left | x = 10 }
    in
        { model | left = newLeft }


updateLeftX x ({ left } as model) =
    { model | left = { left | x = x } }


updatedModel2 =
    updateLeftX 11 model


main =
    div []
        [ div [] [ text <| toString model ]
        , div [] [ text <| toString updatedModel ]
        , div [] [ text <| toString updatedModel2 ]
        ]

Examples from https://groups.google.com/forum/#!topic/elm-discuss/CH77QbLmSTk

-- nested record updates are not allowed
-- https://github.com/elm-lang/error-message-catalog/issues/159
   updatedModel3 =
        { model | left = { model.left | x = 12 } }

-- The .field shorthand does not for the update syntax
-- https://lexi-lambda.github.io/blog/2015/11/06/functionally-updating-record-types-in-elm/
   updateInt : Int -> (data -> Int) -> data -> data
   updateInt val accessor data =
       { data | accessor = val }


   updatedModel4 =
       { model | left = updateInt 13 .x model.left }

Upvotes: 0

Chad Gilbert
Chad Gilbert

Reputation: 36375

sp2 =
    let
        p2 = { p | x = 42 }
    in
        { sp | p = p2 }

Upvotes: 3

Related Questions