shahryarjb

shahryarjb

Macro check is there a specific function inside a module

Hi everyone, I want to create a macro, and this macro should check is there a specific function inside the module is using this macro or not?
for Example it checks 2 functions like: validator and main_validator, if yes, the macro uses it to create a function named builder

the builder uses these functions. when user call the macro it creates for it the builder function.

Code example

defmodule AaMacro do
  defmacro test_macro(opts \\ [], do: block) do
    quote do
      # It should check is there function I want if yes
      
      def builder(props) do
        __MODULE.validator(props)
      end
    end
  end
end

defmodule AaModule do
  use AaMacro
  
  def validator(prop) do
    :ok
  end
end

Based on my knowledge and have read this post, the __info__ function is not working in compile time

https://forum.elixirforum.net/t/how-to-access-module-attributes-in-a-macro-outside-quote/

So how can check and do this problem?
Thank you in advance

Most Liked

al2o3cr

al2o3cr

This is hard to do because you shouldn’t do it.

There’s no difference between these three functions in normal Elixir code:

def many_heads(:foo, x), do: ...
def many_heads(:bar, x), do: ...

def one_head(arg, x) do
  case arg do
    :foo -> ...
    :bar -> ...
  end
end

def one_head_with_a_function(arg, x) do
  inner_function(arg, x)
end

defp inner_function(:foo, x), do: ...
defp inner_function(:bar, x), do: ...

It would be impractical / impossible to write a macro that checks what you’re trying to check that treats all of many_heads, one_head and one_head_with_a_function alike - but it would be very confusing to write a macro that treats them differently.

The usual practice in this situation would be to split the function so that independently-overridable pieces have distinct names - url_validator(...) instead of validator(:url, ...).

al2o3cr

al2o3cr

AaModule.__info__/1 isn’t even going to be defined until after AaModule is compiled.

What would test_macro generate if validator wasn’t defined?

One option would be to have builder check (at runtime) that function_exported?(__MODULE__, :validator, 1) is true, and do something else if it isn’t.

A way to do it at compile-time would be something like what GenServer does to supply a default init callback:

vfsoraki

vfsoraki

You can also require the modules that need to implement validator and main_validator to have a behavior.

This isn’t solid, but if these modules are to use your own custom module, you can force them to have this behavior in __using__ macro and then you’d get warnings (or errors, if you pass —warnings-as-errors to mix) when they don’t implement required functions.

Where Next?

Popular in Questions Top

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
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 record...
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
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
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

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
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
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement