Conditional compilation and macro

Hej,

Sorry for flooding a bit today, but I’ve another question.

When I’m building for js, I also run a macro function that belong to a class : macro public function foo(){...}
If I surround this class with #if js, the compiler can’t find it “Type not found”.
But this class is called form js, it’s not yet in “macro” context.
I thought macro context is only when a macro function call another macro function (without the keyword macro…)
It’s a bit confusing, is it only me ?

The macro context is defined when a macro function is run.

If the compiler knows the foo is a macro, for instance it’s referenced in a @:build, then it’ll load your file in the macro context, see the #if js and discard it, try to find the class in the rest, but there’s nothing there so you have the Type not found error.

A pattern I’ve used is:

#if macro
// Macro specific imports
#else
// Normal imports
#end

#if !macro
class Foo {
    // Normal class
}
#else
class Foo {
    public static macro function foo() {
        // The macro
    }
}
#end

Or you could do

#if macro
// Macro specific imports
#end

// Normal imports

class Foo {
    // Normal parts of the class

#if macro
    public static macro function foo() {
        // The macro
    }
#end
}

Thanks for your reply Valentin,

Knowing that the compiler does something special when a function is macro (with macro keyword) and “put it in macro context”, it makes sense.

But without that, for me, logically it was not in a macro context YET, it was just in the js context, and then if my macro function call another function, yes it is in macro context.

There are many things to know like that about what compiler does behind the scene

Anyway thanks for the explanation