vrod

vrod

This is my first time I am looking at macros. I think I understand they are something like “pre generation” of code. Maybe something like using PHP templates to make Javascript code – they make Elixir code before it runs? OK this is maybe a bad example.

What I am trying to do is this: I have multiple functions that are almost all the same. Each one gets a thing from an API. Each one is the same, but each one returns a different struct. All of them look like this:

defmodule Thing1Resolver do
   def get_one(args) do
     data = get_from_api(args)
     struct(Thing1, args)
   end
end

The only difference is the module Thing1 is different for each function (Thing2, Thing3, Thing4). Can I write a macro to use and build functions like this? So maybe my resolver modules look like

defmodule Thing1Resolver do
    use Shared, struct: Thing1
end

Is this possible? Maybe it is a bad idea. I want to understand how it works and then I can decide.

Thankyou

Showing Posts 1 to 8

John-Goff

John-Goff

Why not just have a function that takes the type of struct? Something like

def get_generic(thing, args) do
  data = get_from_api(args)
  struct(thing, args)
end

Also note with this code you’re not using the API result for anything, and it’s essentially doing nothing, assuming there are no side effects from that API call.

kokolegorille

kokolegorille

You could adapt something like…

defmodule Demo do
  for x <- [1, 2, 3] do
    def unquote(String.to_atom("get_#{x}"))(), do: unquote(x)
  end
end

# then

iex> Demo.get_1
1
iex> Demo.get_2
2
iex> Demo.get_3
3

But it won’t work because You will have functions with same signature… You need to do a little bit more work, like pattern match in functions arguments.

BTW Your code won’t work for multiple reasons…

defmodule Thing1Resolver do
  # There is no way to distinguish signature
   def get_one(args) do
     # You define data, but don't use it
     # get_from_api => side effect!
     data = get_from_api(args)
     # I suppose args should be data
     struct(Thing1, args)
   end

  # As You see, distinction is only inside the function body.
  def get_one(args) do
    struct(Thing2, args)
  end
end

How to solve this? You could call get_from_api outside the function, and pass data as argument.

You could solve with…

defmodule Thing1Resolver do
  def get_one(thing, data), do: struct(thing, data)
end

# and call

data = get_from_api(args)
Thing1Resolver.get_one(any, data)

But if You look at this, well, get_one is just the same as calling struct :slight_smile:

al2o3cr

al2o3cr

You could use macros for this, but since it’s really just substituting a value for thing it’s not a great fit.

Macros are really powerful when you want a piece of code to generate multiple related functions / data, or code that depends on the whole module’s settings. For instance, the Ecto.Schema.schema macro generates __changeset__ and __schema__ functions after all the schema fields are defined along with setting many module attributes.

vrod

vrod OP

I think I was not clear. My code is only a simplification. I do not know if I wish to use macros or no, I want to however see an example of how to pass an argument to the use function something like phoenix controller use Web, :controller but instead to pass a struct name. I am not wanting to pass the struct as a function argument but I cannot explain this easily (sorry my English is bad). Mostly I want to learn better how to use a macro that takes some argument.

Marcus

Marcus

In theory you can do it that way, but it is not the right way. Or I don’t understand you right. Here is an example how things can work. Please note that this example is not good under several aspects.

defmodule FooBar.Thing1 do
  defstruct [val: nil, type: This.Is.Thing1]
end

defmodule FooBar.Thing2 do
  defstruct [val: nil, type: This.Is.Thing2]
end

defmodule FooBar.Share do
  defmacro __using__(opts) do

    quote do
      def new(args) do
        struct(unquote(opts[:struct]), args)
      end
    end
  end
end

defmodule FooBar.Creator do
  defmacro defcreate(thing) do
    quote do
      def create(args) do
        struct(unquote(thing), args)
      end
    end
  end
end

defmodule FooBar.Thing1Resolver do
  use FooBar.Share, struct: FooBar.Thing1
  import FooBar.Creator

  defcreate(FooBar.Thing1)
end

defmodule FooBar.Thing2Resolver do
  use FooBar.Share, struct: FooBar.Thing2
  import FooBar.Creator

  defcreate(FooBar.Thing2)
end

That can be used like:

iex(1)> FooBar.Thing1Resolver.new(val: 55)
%FooBar.Thing1{type: This.Is.Thing1, val: 55}
iex(2)> FooBar.Thing2Resolver.new(val: 44)
%FooBar.Thing2{type: This.Is.Thing2, val: 44}
iex(3)> FooBar.Thing1Resolver.create(val: 55)
%FooBar.Thing1{type: This.Is.Thing1, val: 55}
iex(4)> FooBar.Thing2Resolver.create(val: 55)
%FooBar.Thing2{type: This.Is.Thing2, val: 55}
vrod

vrod OP

Thank you, this is useful! I think this helps me undrestand how the macros work. Ecto is too complicated I can’t follow it.
But why is this not the right way?

al2o3cr

al2o3cr

Because this:

defmodule FooBar.Thing1Resolver do
  use FooBar.Share, struct: FooBar.Thing1
  import FooBar.Creator

  defcreate(FooBar.Thing1)
end

is harder to read than this:

defmodule FooBar.Thing1Resolver do
   def new(args) do
    struct(FooBar.Thing1), args)
  end

  def create(args) do
    struct(FooBar.Thing1), args)
  end
end

For instance, it’s considerably less obvious the functions generated in Thing1Resolver in the first version are identical since their definitions are split among two modules and use arguments differently.

Another approach to consider: put as little code in the macro as possible and call out to the real implementation. This is part of what happens when you say use Ecto.Repo in your application:

defmodule FooBar.Share do
  defmacro __using__(opts) do
    struct_name = opts[:struct]

    quote do
      def new(args) do
        FooBar.Share.new(unquote(struct_name), args)
      end
    end
  end

  def new(struct_name, args) do
    struct(struct_name, args)
  end
end

defmodule Wat do
  use FooBar.Share, struct: Wat

  defstruct [:val]
end

Wat.new(val: 1)

Here the macro generates a function which adds in the argument passed to use but delegates all the functionality to a function defined on FooBar.Share. There’s a tiny compilation space/performance improvement since less AST is generated, and there’s less thinking about tricky things with unquote.

vrod

vrod OP

I see what you are saying, I am only looking for a simple example – showing how to generate 1 function is better example for teaching than the 2 modules (I confess I did not understand this). There is big differences in what is good example for teaching and how to use the techniques in real code but I understand your concern. Thank you!

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews