cpiemontese

cpiemontese

Injecting code in macro with references to outer scope

Hello!

I’m working with macros and I am trying to inject some code into a use macro to be used “as-is” but I’m having some trouble making it work.

I would like to be able to define a __using__ macro which takes some options and creates a function that can use those options “as-is” i.e. if I pass Application.get_env(...) it will get the env at the time of the call, something like

defmodule HypotheticMod do
  # Let's assume this will create a foo function
  # which returns the value of Application.get_env(:app, :foo, 0)
  use InjectMacro, option: Application.get_env(:app, :foo, 0)

  def get_foo_from_env(), do: foo()
end

The first version I tried was this

Application.delete_env(:app, :test)

defmodule Foo do
  defmacro __using__(opts) when is_list(opts) do
    foo = Keyword.get(opts, :foo)

    quote do
      def func() do
        unquote(foo)
      end
    end

  end
end

defmodule Bar do  
  a = 1
  use Foo, foo: Application.get_env(:app, :test, a)
end

a = 2
Application.put_env(:app, :test, a)

Bar.func()

and I expected Bar.func() to output 2, but I get the following error:

function a/0 (expected Bar to define such a function or for it to be imported, but none are available)

Next I tried this

Application.delete_env(:app, :test)

defmodule Foo do
  defmacro __using__(opts) when is_list(opts) do
    foo = Keyword.get(opts, :foo)

    quote bind_quoted: [foo: foo] do
      def func() do
        unquote(foo)
      end
    end

  end
end

defmodule Bar do  
  a = 1
  use Foo, foo: Application.get_env(:app, :test, a)
end

a = 2
Application.put_env(:app, :test, a)

Bar.func()

which returns 1 for some reason I don’t understand.

I am surely missing something, can anyone help me?

Thanks :pray:

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement