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
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
John-Goff
Why not just have a function that takes the type of struct? Something like
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
You could adapt something like…
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…
How to solve this? You could call get_from_api outside the function, and pass data as argument.
You could solve with…
But if You look at this, well, get_one is just the same as calling struct
al2o3cr
You could use macros for this, but since it’s really just substituting a value for
thingit’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.schemamacro generates__changeset__and__schema__functions after all the schema fields are defined along with setting many module attributes.vrod
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
usefunction something like phoenix controlleruse Web, :controllerbut 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
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.
That can be used like:
vrod
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
Because this:
is harder to read than this:
For instance, it’s considerably less obvious the functions generated in
Thing1Resolverin 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.Repoin your application:Here the macro generates a function which adds in the argument passed to
usebut delegates all the functionality to a function defined onFooBar.Share. There’s a tiny compilation space/performance improvement since less AST is generated, and there’s less thinking about tricky things withunquote.vrod
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!