spacebat

spacebat

Constructing qualified function calls in a macro

I’m writing module that will accept a function name and arguments looking like foo(bar, baz) - as Kernel.def/2 does but without a body, and given M is known at compile time I want it to expand to:

def foo(bar, baz), do: M.foo(bar, baz)

I can see how to do this by manipulating the AST. The :foo in:
{:foo, [], [{:bar, [], Elixir}, {:baz, [], Elixir}]} # => foo(bar, baz)
…becomes…
{:., [], [{:__aliases__, [alias: false], [:M]}, :foo]}

In Lisp you’d be able to do something like this using the quote/unquote forms - is it possible in Elixir?

PS. I don’t think defdelegate is right for this as the function body generated by the macro may be more complicated - this is just the part I couldn’t see a nice approach for.

Marked As Solved

kip

kip

ex_cldr Core Team

Yes, very similar to lisp.

defmacro mydef(fun, args) do
  quote do
    def unquote(fun)(unquote(args)) do
      MyModule.unquote(fun)(unquote(args))
    end
  end
end

This is an overly simplistic implementation but it might be enough for your needs.

Take a look at the implementation of the defdelegate macro and you’ll see it’s basically doing the same thing as the code above - only more generalised.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement