Structo (structo v0.1.1)

View Source

JavaScript-style object constructors.

This library allows you to construct maps and structs like this:

~m{a, b, c: d}

Instead of:

%{a: a, b: b, c: d}

And it works in matches too:

iex> ~m{a, b: 2} = %{a: 1, b: 2}
iex> a
1

Summary

Functions

While it is a giant hack, this library can semi-work for structs as well. Just use Structo in your module

This sigil acts as a shorthand for constructing maps with JavaScript-like syntax.

Functions

__using__(opts)

(macro)

While it is a giant hack, this library can semi-work for structs as well. Just use Structo in your module:

defmodule MyStruct do
  use Structo
  defstruct [:a, :b]
end

And then construct your structs like this:

iex> import MyStruct
iex> a = "wibble"
iex> ~MYSTRUCT{a, b: "wobble"}
%MyStruct{a: "wibble", b: "wobble"}

Unfortunately, the module name has to be upcased, due to Elixir's restrictions on sigil names.

sigil_m(arg, list)

(macro)

This sigil acts as a shorthand for constructing maps with JavaScript-like syntax.

Examples

iex> import Structo
iex> a = 1; b = 2; d = 3
iex> ~m{a, b, c: d}
%{a: 1, b: 2, c: 3}