Oliver

Oliver

Question about macro expansion and compilation unit

I have a macro that expands into a module definition. The use case is that we take a testcase defined in one elixir module (parent) and modify some parameters and generate a new module that calls parent where needed but as a new, separate testcase. (This works fine.)

Files then look like this:

require ParametrizedTestcase

ParametrizedTestcase.new(<keyword list>) # let's call this B

ParametrizedTestcase.new(<keyword list>) # let's call this C

So this would for example create new variation B and C of an original testcase A. Or C could even be a variation of B.

When trying to extend it I run into limits defined by when elixir expands macros vs when the modules are compiled.

It’s fine if C statically calls B in its implementation.

It’s fine if C checks in the macro itself if a function in a generated module is exported if and only if that module is not in the same compilation unit/file. (B must be moved.)

The check for a function being exported simply returns false if C tries, in the macro, to check if something is defined in B.

I assume this is because all macros in the compilation unit are expanded together before the modules are compiled.

It seems separating the calls into separate files would avoid this, but right now I have a lot of these testcases organized by files acting as categories, something my users expect. (The joys of legacy.)

My questions are:

  1. Is there a way to enforce compilation of what has been expanded so far before continuing? (Like: I need B to be compiled before C expands.)

OR

  1. Is there a way to prevent the user of the API to invoke the same macro twice in the same compilation unit to enforce splitting invocations over separate files?

The rationale for 2. would be that if I cannot make this work in one file, I want to prevent the possibility of generating erroneous definitions when users revert to adding multiple definitions in one file again. If the macro’s correctness depends on B being compiled before C is expanded, I want to guarantee there’s no two ways to approach this.

Thank you!

Marked As Solved

mudasobwa

mudasobwa

Creator of Cure

I have a macro that expands into a module definition.

Assuming it expands to defmodule, I believe Module.create/3 instead would do.

Also Liked

LostKobrakai

LostKobrakai

The unit of compilation is files not modules afaik. There‘s no guarantee that there‘s modules in an elixir file, nor how many.

Oliver

Oliver

@mudasobwa This is indeed the solution I was seeking. Thank you very much!

I did some experiments with it and it does what I want.

I would have just never guessed that from the documentation.

While I suspected it actually did what I wanted because it returned bytecode, it actually doesn’t mention it.

Something along the lines of "It also instantly compiles and loads the module." would be a great addition there.

Definitely a really cool thing!

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
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
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement