This would be really useful. Two things to consider.

Should the bang version really raise for redundant options?. If foo(opts)
wraps bar(opts) with extra options, foo() needs to clean up the opts before
passing them down which is some of the most annoying plumbing code out
there. Also, if a function removes an option, the error is only reported at
runtime. It's a breaking change in terms of the function's API, but I'm not
a fan of the raise in this case. Maybe a non-bang version is the solution
here.

What do you think about supporting pattern matching? %{opt1: opt1, opt2:
opt2} = <something>!(opts, [:opt1, :opt2]) looks pretty good if you need to
access multiple options or use an option multiple times. Would be nice to
be able to do this without the need for Enum.into() or Enum.sort(). Not
sure what "something" should be - maybe something opts specific ("opts!"
maybe?).

Seems like my concerns can be addressed with Keyword.merge() and
Map.merge(), but streamlining this common use case would be great.

Best,
Stefan

pon., 12 lip 2021 o 10:20 José Valim <jose.va...@dashbit.co> napisał(a):

> I would propose to add this to the Kernel module, to mirror the struct!
> helper. But adding to Keyword also works. Thoughts?
>
> On Mon, Jul 12, 2021 at 8:38 AM Paulo Valente <polvale...@gmail.com>
> wrote:
>
>> One common problem we face when dealing with keyword list opts is
>> validating options passed and setting default values to said keyword.
>>
>> This commonly yields code like:
>>
>> def f(x, opts \\ []) do
>>
>> opt_1 = opts[:opt_1] || :default_1
>> opt_2 = opts[:opt_2] || :default_2
>> ...
>> end
>>
>> Nx.Defn.Kernel has an useful helper for these cases (
>> https://github.com/elixir-nx/nx/blob/c8353de695a3d70dc6d518e0ab2ec832faa7df68/nx/lib/nx/defn/kernel.ex#L741
>> ).
>>
>> Perhaps this helper could be included in the Keyword module, along with a
>> non-rasing version of some kind?
>>
>> --
>> 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-core+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/elixir-lang-core/09f8df11-459a-44a6-aaf2-34462069d8ccn%40googlegroups.com
>> <https://groups.google.com/d/msgid/elixir-lang-core/09f8df11-459a-44a6-aaf2-34462069d8ccn%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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-core+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2B4tjxfOhvZ2ky%3Dw-u%2Bpfe0xmWEhGRQBxZS8a0fYzgeTQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2B4tjxfOhvZ2ky%3Dw-u%2Bpfe0xmWEhGRQBxZS8a0fYzgeTQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/CACzMe7ZZPHM8nOp6yFBo9QSnPjp0u%2BcQ%3Db5eDdV8fpb6M0zTnw%40mail.gmail.com.

Reply via email to