I am sure this has been asked a million times already but I find myself coming back to this problem again and again, so I would like to ask the community about the recommended way to handle this, so I can stop thinking about it.
Consider this model.
model = { foo = { bar = { baz = "hello world" } } }
I want to update foo.bar.baz
. How should I do it?
{ model | foo.bar.baz = "hello again" }
wont work. I think, foo.bar
is synonymous with .bar foo
, so it makes sense that I cannot use it on the left side of =
.
Right now, I would write the update function like that:
update message model =
case message of
Message ->
let
foo_bar =
foo.bar
new_foo_bar =
{ foo_bar | baz = "hello again" }
foo =
model.foo
new_foo =
{ foo | bar = new_foo_bar }
in
( { model | foo = new_foo }, Cmd.none )
Generally, I would put every case in its own function to keep the update function itself readable. And I could of course write setter functions that contain alle the record disassembly and reassembly. But that’s just moving this code to another place, it still has to be written.
So, is there a better, more concise way to do this?
Or how about auto-generated setter functions?