defmodule Test do
def test(a, opts \\ []) when is_list(opts) do
with opts <- opts |> Enum.into(%{foo: :a}) do
test(a, opts)
end
end
def test(a, opts) do
IO.inspect(a)
IO.inspect(opts)
true
end
end
Test.test(1)
#=> 1
#=> %{foo: :a}
# default opts match on the guard as a list
Test.test(1, foo: :b)
#=> 1
#=> %{foo: :b}
# values are not overwritten
Created
February 24, 2023 16:15
-
-
Save eprothro/d35c504aaf16413bb19d7d3fbc3c4918 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://elixirforum.com/t/whats-the-most-idiomatic-way-to-require-a-key-in-a-keyword-list-or-make-it-optional/9577