alichoopani

alichoopani

Call external module funcs inside generated functions in macro

hello everyone.
I am trying to learn metaprogramming and start with simple project. in this road I this weird situation.
I want to implement interface like action_fallback macro in Phoenix controller(inside UseSample module).


defmodule GuardModule do
  def test() do
    IO.inspect("TEST")
  end
end

defmodule Sample do

  defmacro guard(module_name) do
    quote do
      @guard unquote(module_name)
    end
  end

  defmacro __using__(_opts) do
    quote do
      import unquote(__MODULE__)
      Module.register_attribute(__MODULE__, :guard, accumulate: :false, persist: :false)

      @before_compile unquote(__MODULE__)
    end
  end

  defmacro __before_compile__(env) do
    custom_guard = Module.get_attribute(env.module, :guard)
    custom_guard.test()
    # quote do
    #   def test_inside_use_sample() do
    #     custom_guard.test()
    #   end
    # end
  end
end

defmodule UseSample do
  use Sample

  guard GuardModule
end

now I have some questions.

  1. in Sample.__before_compile__ macro i can call external module functions. but i can’t generate function that call this external function inside its body(commented section not compile). how can I do that?
  2. in this situation GuardModule compiles before others because it’s in the beginning of file. how can I force elixir compiler to compile this module before Sample module?
  3. and finally am i have right solving strategy for this problem or should do something else?

thanks a lot.

Most Liked

rvirding

rvirding

Creator of Erlang

There are no problems for you to load in your version of that module again. There is no way to stop anyone from loading in new versions of any module. However there are some properties of code handling you should be aware of:

  1. There can at most be 2 versions of a module at the same time, the current and the old. When a new version is loaded in the old version is automatically and irrevocably deleted, the current version becomes the “new” old and the the new one becomes the “new” current.

  2. The system doesn’t try and be smart and say that if you already have the version of the module loaded it won’t reloaded It does what you tell it to.

  3. The code handling also kills all the processes using the old version of a module when that is deleted.

  4. This leads to some fun things. If you reload a module twice you are guaranteed to kill all processes running it regardless of which version they are running.

  5. When you call a function in a module which version you call depends on how it is called. If you make a local call you always get the same version while if you make a fully qualified call, Module.function, you always get a the current version. There is no way to explicitly call functions in the old versions from the “outside”.

Sorry for a quick description of code handling in the BEAM/Erlang/Elixir.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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

We're in Beta

About us Mission Statement