3 ms·
> alice.address.city = "Los Angeles"; > The first issue in Haskell is that we can't mutate alice; we instead have to return a new Person value with the updated
by okasaki 4y ago
> alice.address.city = "Los Angeles";
> The first issue in Haskell is that we can't mutate alice; we instead have to return a new Person value with the updated city.
I don't get it. Ok, so it can't mutate, but why can't haskell have an easy syntax and just return a copy with the updated value? Why does it need this very convoluted way for updating records?
eg you could have {{ }} denote an "update block", and this has some special syntax to change values, add or remove items from lists/dicts and similar
let new_alice = {{ alice.address.city = "Los Angeles",
alice.cart.items += 123 }}
- cercatrova 4y agoThis is basically what Immer [0] does for Javascript apps if you use immutable-like libraries like Redux which expect a new object created every time a property changes so that it can detect that it's changed and propagate that to any observing components. Perhaps Haskell needs something similar. [0] https://github.com/immerjs/immer https://github.com/immerjs/immer
- hither_shores 4y agoThat's exactly what lenses do: let new_alice = set (address . city) "Los Angeles" $ over (cart . items) (+ 123) alice
- grumpyprole 4y agoYes it could. But lenses offer much more than that. They are sometimes called "functional references", because a lens can be used for both reading and updating; and can be passed around. They can also be composed and combined in arbitrarily complex ways, e.g a lens into a map, option or a list.
- operator-name 4y agoIt does, but was only introduced more recently: https://ghc-proposals.readthedocs.io/en/latest/proposals/0282-record-dot-syntax.html#record-dot-syntax https://ghc-proposals.readthedocs.io/en/latest/proposals/028... Lenses are still more powerful as they can be composed. Consider your (and the record dot) syntax for having to modify alice.cart.items on one branch of the conditional and alice.cart.discount on another branch of the conditional.