Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Proposal: Map.validate/2

69 views
Skip to first unread message

Rudolf Manusadzhian

unread,
Oct 18, 2024, 12:22:07 AM10/18/24
to elixir-lang-core
It's been a couple of years since Erlang started favoring maps for options to keywords (proplists how it's called there). See https://erlangforums.com/t/handling-options-in-erlang-otp-apis/1133

Recently in an elixir project I tried to also use map for options but faced the lack of the analog of Keyword.validate!/2 which is very handy!

I propose adding a similar function to Map as well.

While I'm not entirely sure about the API. Here I put together a commit where I adapted a lot from Keyword.validate/2


The result looks like this:

Screen Shot 2024-10-17 at 11.08.01 PM.png

WDYT?

José Valim

unread,
Oct 18, 2024, 1:17:23 AM10/18/24
to elixir-l...@googlegroups.com
We don't plan to add this because, although passing options may be common in some Erlang libraries, it is not the case in Elixir, and we don't want to necessarily push folks in that direction. Thank you.

--
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/e8094862-56a7-4550-9cc9-e17dbe395daan%40googlegroups.com.

Michael Neumann

unread,
Oct 18, 2024, 6:14:10 AM10/18/24
to elixir-l...@googlegroups.com

Hey Rudolf,

Code looks super nice!

Maps can have arbitrary keys, not just atoms. This is in contrast to Keyword lists, which are defined on atom keys. So, I'd expect that Map.validate would work on any type of key.

Regards,

Michael


--

Generic Jam

unread,
Oct 18, 2024, 6:14:10 AM10/18/24
to elixir-l...@googlegroups.com
There are already Structs and NimbleOptions https://github.com/dashbitco/nimble_options which kind of do the same thing.
Maybe it's still a good idea but it is totally doable as is.


--

Rudolf Manusadzhian

unread,
Oct 18, 2024, 10:10:32 AM10/18/24
to elixir-lang-core
Actually, right! I could use struct/2 from Kernel - to convert to an internal struct it would validate the keys and provide with the defaults!
Anyways, it's good to know the official stance on the subject, and I'd better use keyword for options instead of maps as it's the idiomatic way.

Thanks everyone!
Reply all
Reply to author
Forward
0 new messages