Aetherus

Aetherus

Destructing assignment

I want to mimic ES6’s destructing assignment using Elixir’s sigil.

What I want to achieve is

~m{foo bar} = %{foo: 1, bar: 2}
foo  #=> 1
bar  #=> 2

So far I have the following code

defmodule DestructingAssignment do
  defmacro __using__(_) do
    quote do: import unquote(__MODULE__)
  end

  defmacro sigil_m({:<<>>, _line, [string]}, []) do
    spec = string
           |> String.split
           |> Stream.map(&String.to_atom/1)
           |> Enum.map(&{&1, {&1, [], Elixir}})  #=> [foo: {:foo, [], Elixir}, bar: {:bar, [], Elixir}]
    {:%{}, [], spec}
  end
end

When I run ~m{foo bar} = %{foo: 1, bar: 2}, pattern matching succeeds. But when I try to read the variable foo, it gives me

warning: variable "foo" does not exist and is being expanded to "foo()", please use parentheses to remove the ambiguity or change the variable name
  iex:9

** (CompileError) iex:9: undefined function foo/0

I guess this is due to the macro hygiene, but as you can see, the sigil implementation doesn’t use quote. How can I inject local variable to the macro’s caller’s scope?

Most Liked

kip

kip

ex_cldr Core Team

I think just one small change is required:

defmodule DestructingAssignment do
  defmacro __using__(_) do
    quote do: import unquote(__MODULE__)
  end

  defmacro sigil_m({:<<>>, _line, [string]}, []) do
    spec = string
           |> String.split
           |> Stream.map(&String.to_atom/1)
           |> Enum.map(&{&1, {&1, [], nil}})  #=> [foo: {:foo, [], nil}, bar: {:bar, [], nil}]
    {:%{}, [], spec}
  end
end
Aetherus

Aetherus

Works like a charm! But here come a new question, what does the last part of an AST node mean?

Last Post!

KristerV

KristerV

can’t edit.

this algo has served me well, but today i’d go for shorter_maps.

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement