ExAequo.KeywordParams (ExAequo v0.2.0) View Source

Tools to facilitate dispatching on keyword parameters, used in contexts like the following

  @defaults [a: 1, b: false] # Keyword or Map
  def some_fun(..., options \ []) # options again can be a Keyword or Map
    {a, b} = tuple_from_params(@defaults, options, [:a, :b])

Merging defaults and actual parameters

Its most useful feature is that you will get a map whatever the mixtures of maps and keywords the input was

iex(0)> merge_params([])
%{}

iex(1)> merge_params([a: 1], %{b: 2})
%{a: 1, b: 2}

iex(2)> merge_params(%{a: 1}, [a: 2, b: 2])
%{a: 2, b: 2}

Strict merging

Not implemented yet

Extracting params from the merged defaults and actuals

iex(3)> defaults = [foo: false, depth: 3]
...(3)> tuple_from_params(defaults, %{foo: true}, [:foo, :depth])
{true, 3}

As defaults are required a missing parameter will raise an Error

iex(4)> try do
...(4)>   tuple_from_params([], [foo: 1], [:bar])
...(4)> rescue
...(4)>   KeyError -> :caught
...(4)> end
:caught

Alternatively on can extract a map

iex(5)> map_from_params([], [hello: "world"], [:hello])
%{hello: "world"}

Link to this section Summary

Link to this section Types

Specs

date_tuple() ::
  {non_neg_integer(), 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12, 1..255}

Specs

lstat_result() :: {:ok, File.Stat.t()} | {:error, File.posix()}

Specs

param_type() :: Keyword.t() | map()