COMMUNITY

Macro instance

Hej,

I’m sorry it’s a question of a lazy guy tonight, just wanna know if it’s possible to have an instance of a class at compile time and use it later in code…
I don’t know if it’s clear.
For example, I would to instanciate a “macro class” let’s say giving it a param. And then use a function of this class so it can retrieve the set param…
Is it possible please ?

Something like that :

class Macro{
    macro var path : String;
    macro public function new( path : String ){
        this.path = path;
    }

    macro public function get(){
        // do something with my var "path"
    }
}

If I’m understanding you correctly, this sounds a bit like this adding the git commit hash sample from the code cookbook.

Hej Allan,
Thanks for your answer, but no, this sample is using a static one. I wonder if we can have an instance of a macro. I think it’s a weired question because maybe it’s non-sense having it because of macro execution order or something like that, but just asking…

Like this?

class Macro {
  public static macro function get(path:String):Expr
    return new MacroClass(path).get();
}
class MacroClass {
  public function new(path) {}
  public function get():Expr {}
}

Thanks Kevin for your answer.

I don’t think it solves my thing. I’ll try to detail what I mean.

Let’s say I have a regular class. In this class I deal with JS and this class is related to a template, let’s say it’s a html file. So basically, in this class I have a lot of js.Browser.document.getElementById( "myID" );
I’ve written a little macro to which one I give the HTML file content and the id I’m looking for in order that my macro checks if this id really exists in this Html file.

So my macro is like that : HtmlFile.getElementById( "templates/my_path.mtt", "my_id" ) and it works fine.

Now, as you can see, I always have to give the path of the Html file to my macro, even if it’s the same Html file that is used all along this regular JS class.

So my question is if I could do something like var myHtmlFile = new MyMacro( "templates/my_path.mtt" );and then, all along my regular JS class, do something like myHtmlFile.getElementById( "my-id" ); which will generate the regular code js.Browser.document.getElementById( "my-id" );if the id exists or throw a compilation error if the id doesn’t exists (as my static macro does now, but without giving always the path to the Html file).

I hope it’s more clear now, and I thanks all that read that and try to answer.
Also if it’s not possible to do what I want this way, maybe there is another “shorter” whey than this static and path and id.

Sorry to come back with that but, does anyone know if it could be possible to do something like that please ?

You cannot create a macro object or something like that, no.

What you could do is maintain a cache in your macro, using a static variable, to not repeat loading/parsing of your html file.

Or you could also create a type using your html file as information, generating members using the ids, and then use HtmlFile<"templates/my_path.mtt">.my_id which would only exist if the id is valid.
It gets more complex but doable.

Though are you seeing some bad performances because of your current implementation?
You can check the time spent in macros by adding --times -D macro_times to your haxe command.

Thanks again Valentin for your reply.
In fact, I have a cache for the content of the file so it’s loaded once and even I parse it once and stock all the ids. So it’s quite fast.
Here what I would to avoid is just to write the path every time. But If it’s not possible it’s ok.
And I have also thought about the type parameter as you suggest, I’ll try to do it !
Thanks ! :wink:

But just to ask, is it technically impossible to have an instance of a macro ? I mean, it’s designed like that or it doesn’t have any sense to have a macro instance ?

Technically speaking, you can have instances in the macro context. But there is no way (syntax) to interact with that in the normal context.

1 Like

Thanks Kevin !
Do you think I could suggest that somewhere ? If it’s not too much work to implement, maybe it could be useful as in cases like here ?

Random thought; Maybe you implement something like this, to build the ids from the template. Code completion from URL - Macros - Haxe programming language cookbook

1 Like

Yes I’ve seen that, it could be another solution Mark.
But having macro instances, I could control different templates :yum:
Thanks Mark !

I could manage several templates using build macro too in fact.
Putting several variables in the class that will have each their ids, yes it’s a good idea

Hej Kevin,

I’ve made a Haxe proposal for macro instance, maybe you could join the discussion, you have a wider view of what is technically doable than I have :