Oliver

Oliver

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!

Showing Posts 1 to 5

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.

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.

mudasobwa

mudasobwa

Creator of Cure

Sure. Launch a custom Mix.Task.Compiler and spawn a process in its run/1 callback implementation. Then trace def trace({remote, meta, YourUsedMod, :__using__, 1}, env) and keep track of what has been already done on that matter.

Oliver

Oliver OP

@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!

mudasobwa

mudasobwa

Creator of Cure

Cool. Then please don’t mind to open a PR to Elixir core adding this wording to documentation.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews