--
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/09f8df11-459a-44a6-aaf2-34462069d8ccn%40googlegroups.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.
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 [...] Maybe a non-bang version is the solution here.
What do you think about supporting pattern matching?
I debated this with the Elixir team and we agreed on the following API:
Keyword.validate! :: keyword | no_return Keyword.validate :: {:ok, keyword} | {:error, unknown_keys, missing_keys}
The same functionality would have to be defined for Map.
The API for Keyword.validate! and Map.validate! would be the exact same as the one found in Nx.Defn.Kernel.keyword!.
Could you please send a PR? Or open up an issue if you can't submit it at the moment.
Thanks!On Monday, July 12, 2021 at 12:46:38 PM UTC+2 polva...@gmail.com wrote:
> I would propose to add this to the Kernel module, to mirror the struct! helper. But adding to Keyword also works. Thoughts?
Makes sense! I suggested Keyword mostly because there's the module already, but since we have struct! in Kernel, it might be better to have keyword! there too. Usage would also be nicer this way.
> Should the bang version really raise for redundant options?
As José said, it would help clean out stray options being passed. I can think of a few cases in which I thought one option was being used, but had either a typo or wrong name altogether (think "end" vs "end_datetime" sort of situation).
However, I can see a less strict version being useful as well. Perhaps there could be a keyword!/3 which accepted options like: keyword!(opts, [extra_keys: true], key1: :default1, key2: :default2)
--
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/9b83a803-c2c1-49e7-a531-56791607bcaan%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/a96fb19a-8148-41dc-8b45-0e2b9fe755b8n%40googlegroups.com.
# TODO: use Keyword.validate when we require Elixir 1.13 if Version.compare(System.version(), ">= 1.13.0") do defp keyword_validate(keyword, values) do Keyword.validate(keyword, values) end else defp keyword_validate(keyword, values) when is_list(keyword) and is_list(values) do validate(keyword, values, [], [], []) end defp validate([{key, _} = pair | keyword], values1, values2, acc, bad_keys) when is_atom(key) do case find_key!(key, values1, values2) do {values1, values2} -> validate(keyword, values1, values2, [pair | acc], bad_keys) :error -> case find_key!(key, values2, values1) do {values1, values2} -> validate(keyword, values1, values2, [pair | acc], bad_keys) :error -> validate(keyword, values1, values2, acc, [key | bad_keys]) end end end defp validate([], values1, values2, acc, []) do {:ok, move_pairs!(values1, move_pairs!(values2, acc))} end defp validate([], _values1, _values2, _acc, bad_keys) do {:error, bad_keys} end defp validate([pair | _], _values1, _values2, _acc, []) do raise ArgumentError, "expected a keyword list as first argument, got invalid entry: #{inspect(pair)}" end defp find_key!(key, [key | rest], acc), do: {rest, acc} defp find_key!(key, [{key, _} | rest], acc), do: {rest, acc} defp find_key!(key, [head | tail], acc), do: find_key!(key, tail, [head | acc]) defp find_key!(_key, [], _acc), do: :error defp move_pairs!([key | rest], acc) when is_atom(key), do: move_pairs!(rest, acc) defp move_pairs!([{key, _} = pair | rest], acc) when is_atom(key), do: move_pairs!(rest, [pair | acc]) defp move_pairs!([], acc), do: acc defp move_pairs!([other | _], _) do raise ArgumentError, "expected the second argument to be a list of atoms or tuples, got: #{inspect(other)}" end defp keyword_validate!(keyword, values) do case keyword_validate(keyword, values) do {:ok, kw} -> kw {:error, invalid_keys} -> keys = for value <- values, do: if(is_atom(value), do: value, else: elem(value, 0)) raise ArgumentError, "unknown keys #{inspect(invalid_keys)} in #{inspect(keyword)}, the allowed keys are: #{inspect(keys)}" end end end
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/43f206e4-6b22-4622-b8d9-1ade01556039n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/b8beea46f50e401bf00961fff49a76f68c360bb3%40hey.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CACzMe7aTSK%3Dpni6gM2xDhXoUKFjJTTZ9cY4TqXniDUtddohA3Q%40mail.gmail.com.