[proposal] Kernel.fetch_in

52 views
Skip to first unread message

Brian Cardarella

unread,
Aug 29, 2016, 7:11:29 AM8/29/16
to elixir-lang-core
When using `with` there are several times I wish I could have reached deeper into a Map or Keyword list in a single line rather than multiple fetch statements. Ideally it would work like this:

```
map = %{foo: %{bar: "baz"}}

%{:ok, "bar"} = fetch_in(map, [:foo, :bar])
:error = fetch_in(map, [:foo, :qux])
:error = fetch_in(map, [:bar])
```

If this is OK I can implement it.

Allen Madsen

unread,
Aug 29, 2016, 8:19:59 AM8/29/16
to elixir-l...@googlegroups.com
+1 from me. Seems consistent with `get_in` and `put_in` and follows
the semantics of `fetch`.
Allen Madsen
http://www.allenmadsen.com
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to elixir-lang-co...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/7daf519b-6122-4aeb-a8bd-7115625f28a9%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Allen Madsen

unread,
Aug 29, 2016, 8:22:25 AM8/29/16
to elixir-l...@googlegroups.com
One small amendment, in the successful case, I think `fetch_in` should
return a tuple instead of a map.
Allen Madsen
http://www.allenmadsen.com

Brian Cardarella

unread,
Aug 29, 2016, 9:39:00 AM8/29/16
to elixir-lang-core, allen.c...@gmail.com
Sorry, I meant to put a tuple as the return type. Yes, no map.

Wiebe-Marten Wijnja

unread,
Aug 29, 2016, 12:38:15 PM8/29/16
to elixir-lang-core
I like this idea. I think it is a good addition. Using `get_in`, it is impossible to tell if the key exists, but with `nil` as value, or of it doesn't exist.
Reply all
Reply to author
Forward
0 new messages