Shorthand provides macros to create or match against maps and keyword lists with atom or string-based keys.
Add shorthand
as a dependency in your project in your mix.exs
file:
def deps do
[
{:shorthand, "~> 1.0"}
]
end
Wherever you would use a map literal, you can use the shorthand macros instead, whether in assignment or as a pattern.
%{conn: conn}
can become m(conn)
.
%{params: %{"email" => email, "password" => password}}
can become m(params: sm(email, password))
.
%{foo: _foo}
can become m(_foo)
.
%{foo: ^foo}
can become m(^foo)
.
You can specify the variable name for the key you are destructuring along with shorthand keys, but like normal function calls, they work as keyword lists at the end.
%{foo: foo, bar: bor, baz: qux}
can become m(foo, bar, baz: qux)
%{foo: bar, baz: baz, qux: qux}
would need to become m(baz, qux, foo: bar)
See the docs for more examples
Shorthand | Equivalent Elixir |
---|---|
m(foo, bar) |
%{foo: foo, bar: bar} |
m(foo, _bar, ^baz) |
%{foo: foo, bar: _bar, baz: ^baz} |
m(foo, bar, baz: m(qux)) |
%{foo: foo, bar: bar, baz: %{qux: qux}} |
m(foo, m(baz) = bar, qux: m(quux)) |
%{foo: foo, bar: %{baz: baz} = bar, qux: %{quux: quux}} |
m(foo, bar = m(baz), qux: m(quux)) |
%{foo: foo, bar: %{baz: baz} = bar, qux: %{quux: quux}} |
Shorthand | Equivalent Elixir |
---|---|
sm(foo, bar) |
%{"foo" => foo, "bar" => bar} |
sm(foo, _bar, ^baz) |
%{"foo" => foo, "bar" => _bar, "baz" => ^baz} |
sm(foo, bar, baz: sm(qux)) |
%{"foo" => foo, "bar" => bar, "baz" => %{"qux" => qux}} |
sm(foo, sm(baz) = bar, qux: sm(quux)) |
%{"foo" => foo, "bar" => %{"baz" => baz} = bar, "qux" => %{"quux" => quux}} |
sm(foo, bar = sm(baz), qux: sm(quux)) |
%{"foo" => foo, "bar" => %{"baz" => baz} = bar, "qux" => %{"quux" => quux}} |
Shorthand | Equivalent Elixir |
---|---|
kw(foo, bar) |
[foo: foo, bar: bar] |
kw(foo, _bar, ^baz) |
[foo: foo, bar: _bar, baz: ^baz] |
kw(foo, bar, baz: kw(qux)) |
[foo: foo, bar: bar, baz: [qux: qux]] |
kw(foo, kw(baz) = bar, qux: kw(quux)) |
[foo: foo, bar: [baz: baz] = bar, qux: [quux: quux]] |
kw(foo, bar = kw(baz), qux: kw(quux)) |
[foo: foo, bar: [baz: baz] = bar, qux: [quux: quux]] |
Shorthand | Equivalent Elixir |
---|---|
st(MyStruct, foo, bar) |
%MyStruct{foo: foo, bar: bar} |
st(MyStruct, foo, _bar, ^baz) |
%MyStruct{foo: foo, bar: _bar, baz: ^baz} |
st(MyStruct, foo, bar, baz: st(MyStruct, qux)) |
%MyStruct{foo: foo, bar: bar, baz: %MyStruct{qux: qux}} |
st(MyStruct, foo, m(baz) = bar, qux: m(quux)) |
%MyStruct{foo: foo, bar: %{baz: baz} = bar, qux: %{quux: quux}} |
st(MyStruct, foo, bar = m(baz), qux: m(quux)) |
%MyStruct{foo: foo, bar: %{baz: baz} = bar, qux: %{quux: quux}} |