COMMUNITY

Debug "You cannot access the js package while in a macro" error

I am working on macros and want to add functionality to for example the ClassType class through extension methods. What I am running into is that when I add a using XXX to my macro class I get a “You cannot access the js package while in a macro” in a completely unrelated class.

Is there any advice on how to debug this and find out why this is happening?

js package is reserved to javascript execution, and not accessible during macro execution.
With a piece of code, it will be easier to see where it comes from :wink:

@aliokan: thanks for the effort. I guess the reason for the error is that the class that uses the js package is included into the compilation. I found out why it is doing this but not sure how to fix it “properly”.

So what I wanted to do in the macro is check whether the current local class (obtained through Context.getLocalClass()) inherits from a given class.

To do this I created something like this:

public function InheritsFrom(classType : ClassType, parentClass : Class<Dynamic>) : Bool
{
	...
}

// Use it as:
InheritsFrom(Context.getLocalClass(), namespace.something.MyClass);

The nice thing about this is that compilation fails if the namespace.something.MyClass changes. The downside is that by doing this the compiler includes MyClass and everything is uses which caused the js package error.

I guess I can do the InheritsFrom function with a string (like “namespace.something.MyClass”) but I was hoping for a bit more elegant approach.

Note: the static extensions do not work in macros so I found another way around this using abstract types.