5 ms·
Nice feature in this release is the addition of `get_in/1` which works with structs. eg. `get_in(struct.foo.bar)` If `foo` returns `nil`, accessing `bar` won't
by neillyons 2y ago
Nice feature in this release is the addition of `get_in/1` which works with structs. eg. `get_in(struct.foo.bar)`
If `foo` returns `nil`, accessing `bar` won't raise.
- mtndew4brkfst 2y agoIt was still possible to do this in prior versions of Elixir, just syntactically noisy. Any layer which isn't a vanilla map needs Access.key, as in: get_in(struct, [Access.key(:foo), :bar])
- skrebbel 2y agoBut that’s way less clear than just if struct.foo do struct.foo.bar else nil end
- mtndew4brkfst 2y agoSure, this snippet is a trivial example and doesn't sufficiently motivate get_in and family, which can be much more powerful when needs aren't so simple. It's also very hard to be as concise with more vanilla syntax when competing with put_in/update_in instead. The pattern and ability to generalize it is valuable IME.