Skip to content

Instantly share code, notes, and snippets.

@eprothro
Created February 24, 2023 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eprothro/d35c504aaf16413bb19d7d3fbc3c4918 to your computer and use it in GitHub Desktop.
Save eprothro/d35c504aaf16413bb19d7d3fbc3c4918 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment