Megadef v0.1.0 Megadef View Source

Utilities to make it easier to exract function parameters from keyword lists or maps.

Elixir encourages you to use keyword lists for optional parameters. The problem is that oyu can’t use pattern matching to extract their value, because users expect to be able to supply parameters in any order.

You’re forced to write something like this:

def f(options) do
  a = Keyword.get(options, :a, 1)
  b = Keyword.get(options, :b, 2)
  c = Keyword.get(options, :c, 3)
  d = Keyword.get(options, :d, 4)
  # ...
end

For some (most?) use cases, it would be better if you could write something like this:

def f([a: 1, b: 2, c: 3]) do
  #...
end

which would desuger into the above. This package adds some macros that make it possible. It defines 4 macros:

For example:

  # The `= options` is required; we need an argument name for documentation purposes
  def f(&[a: 1, b: 2, c: 3] = options) do
    # ...
  end

It will expand into:

def f(options) do
  a = Keyword.get(options, :a, 1)
  b = Keyword.get(options, :b, 2)
  c = Keyword.get(options, :c, 3)
  # ...
end

Reference

The megadef macro supports parameter extraction from maps or keyword lists. Both required and optional parameters are supported.

Keyword list with optional parameters:

megadef f(&[a: 1, b: 2] = options) do
  {a, b}
end

If all parameters in the keyword list are optional, you should make the list itself optional:

megadef f(&[a: 1, b: 2] = options \\ []) do
  {a, b}
end

Keyword list with optional and required parameters:

megadef f(&[a, b: 2] = options) do
  {a, b}
end

In this case, you haven’t provided a default value for a. This means that a is a required parameter. The parameter will be fetched by Keyword.fetch!/2 from the keyword list, and if it doesn’t exist it will raise an error.

Internals

These macros use the unary Kernel.&/1 operator. This operator has very low precedence, which means that it encloses the whole argument. For example: &[a: 1, b: 2] = opts \\ [] is equivalent to &([a: 1, b: 2] = opts \\ []). The semantics are backwards-compatible with the semantics inside argument lists of functions defined with Kernel.def/2, because you can’t use &... in a pattern.

If you want, you can redefine def(macro)?(p)? so that they call mdegadef(macro)?(p)?. You probably shouldn’t, though, because it can make your code more confusing to read.

Link to this section Summary

Functions

Same thing as Kernel.def/2 but with extended support for matching arguments with keyword lists and optional values in maps

Same thing as Kernel.defmacro/2 but with extended support for matching arguments with keyword lists and optional values in maps

Same thing as Kernel.defmacrop/2 but with extended support for matching arguments with keyword lists and optional values in maps

Same thing as Kernel.defp/2 but with extended support for matching arguments with keyword lists and optional values in maps

Link to this section Functions

Link to this macro megadef(definition, body \\ []) View Source (macro)

Same thing as Kernel.def/2 but with extended support for matching arguments with keyword lists and optional values in maps.

Link to this macro megadefmacro(definition, body \\ []) View Source (macro)

Same thing as Kernel.defmacro/2 but with extended support for matching arguments with keyword lists and optional values in maps.

Link to this macro megadefmacrop(definition, body \\ []) View Source (macro)

Same thing as Kernel.defmacrop/2 but with extended support for matching arguments with keyword lists and optional values in maps.

Link to this macro megadefp(definition, body \\ []) View Source (macro)

Same thing as Kernel.defp/2 but with extended support for matching arguments with keyword lists and optional values in maps.