riccardomanfrin

riccardomanfrin

Quote, defmacro and modules

I’m trying to understand how macros and module definitions interact with each others and I’m having few cases which I don’t understand. The below example has some pseudo doctest to show my doubts.

What I need is to support case 5. Thanks for the support in advance

defmodule Foo do
  @doctest """
  Case 1 OK: Trivial (and useless) case.. (Foo.Case1)

      iex> Foo.Case1.foo
      "local stuff"

  Case 2 OK: Injects the Case2 in WrapMod and uses its args
  WARNING: comment Case4 for this to work (head scratching)

      iex> WrapMod.Case2.foo
      [foo: "bar"]

  Case 3 FAILED!!!! Injects used args into the Foo.Case3.foo function

  Error:

    example.ex:24: undefined function args/0 (expected Foo.Case3 to define
    such a function or for it to be imported, but none are available)

  Desiderata:

    iex> Foo.Case3.foo
    [foo: "bar"]

  Case 4 KINDA NOT OK: Super clunky combo of case 2 and 1

  It works, but it HIDES!!!!! Case 2: Call to `WrapMod.Case2.foo` is not available
  anymore

      iex> WrapMod.Case4.foo
      "local stuff"
      iex> WrapMod.Case2.foo
      UndefinedFunctionError

  Case 5 FAILED: Hyper clunky combo of case 2 with case 3 <- which is not working :(

  This is where I'd want to get to: I would like to be able to invoke
  and use the evaluated Foo.Case3.foo from within the quoted  Case5 quoted module code

  """

  defmacro __using__(args) do

    #Case 1
    defmodule Case1 do
      def foo() do
        "local stuff"
      end
    end

    #Case 2
    quote do
      defmodule Case2 do
        def foo() do
          unquote(args)
        end
      end
    end

    #Case 3
    #defmodule Case3 do
    #  def foo() do
    #    quote do
    #      unquote(args)
    #    end
    #  end
    #end

    # Case 4: comment me to get back WrapMod.Case2.foo which is otherwise no more available!!!
    quote do
      defmodule Case4 do
        def foo() do
          Case1.foo()
        end
      end
    end

    #quote do
    #  defmodule Case5 do
    #    def foo() do
    #      Case3.foo()
    #    end
    #  end
    #end

  end
end

defmodule WrapMod do
  use Foo, foo: "bar"
end

For the records, case 3 works if I change it like this:

    #Case 3
    defmodule Case3 do
      def foo() do
        quote do
          var!(args)
        end
      end
    end

but this is giving me

iex(1)> Foo.Case3.foo
{:var!, [context: Foo.Case3, import: Kernel], [{:args, [], Foo.Case3}]}

which is clearly not what I want

Most Liked

fuelen

fuelen

As in regular functions, macro returns the last expression.
It is not necessary to wrap two quote expressions into a list. You can simply wrap both module definitions into 1 quote block:

quote do
  defmodule Case2 do
    def foo() do
      unquote(args)
    end
  end

  defmodule Case4 do
    def foo() do
      Case1.foo()
    end
  end
end

Where Next?

Popular in Questions Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement