Petr

Petr

Would one-time self-replacing macro/compile-time-function instead of CLI generators be possible?

There is an example in Ash Framework of adding a resource Album into Music domain
mix ash.gen.resource Tunez.Music.Album --extend postgres. It generates a new module Album and modifies Music module – adding the Album into the domain.

I wondered if it would be possible to rather have a compile-time function (macro-ish) that would replace itself this one time during compilation and modify other modules as needed? It would allow developers not to deal with CLI but just use Elixir functions instead in the IDE environment having its usual features (docs, autocompletion, etc.)

To make it a bit more clear, I am imagining something like:

  • Add add_resource "Artist", extend: "postgres" in the defmodule Tunez.Music.
  • During compilation, add_resource would get called replacing itself and modifying Tunez.Music module, and creating Tunez.Music.Artist module
    • It would add resource Tunez.Music.Artist in the resources do ... block.
  • Which means it would have to recompile Tunez.Music after the function add_resource was executed and add Tunez.Music.Artist to the compilation queue.

What do I expect? To hear where this idea breaks down and to learn something new from that about Elixir. (Or possibly, how to achieve this.)

Why am I asking: I am exploring Elixir and its ecosystem and libraries (looks all very promising). I like to learn about languages/frameworks by trying to push at their limits. I would like to have the natural Elixir interface to generate the code instead of dealing with CLI tools myself.

If feasible, I would try to do this to get my hands dirty.

Thanks.

Most Liked

zachdaniel

zachdaniel

Creator of Ash

I think it would be technically doable, but would be relatively complex, and might be able to be done better via things like editor snippets etc. but yeah a macro could call arbitrary code at compile time, including code that replaces it in its own source file.

LostKobrakai

LostKobrakai

Wouldn’t even need to be a macro. When there’s no AST to be replaced with other AST it could potentially even be a function call. Though not sure how much “context” one gets about where exactly a call originated to replace that line.

Should also be quick to “validate”

# lib/commands.ex
Mix.Task.run("ash.gen.resource", ["Tunez.Music.Album", "--extend", "postgres"])

File.write(__ENV__.file, File.stream!(__ENV__.file, :line) |> Enum.at(-1))

Where Next?

Popular in Questions Top

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 a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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