Haxe got Macros !
Haxe has been evolving from the beginning. At first there was only JS/Flash8/Neko platforms support, and it now support Flash9, PHP, and C++ platforms (the later enabling to target all the new devices such as iPhone, Android, and much more such as Palm)
But more than new targets, Haxe is also getting new features. 2.06 release added Metadata, and the SVN version just got Macros
Macros in Haxe are a way for developers to enhance and customize the capabilities of the Haxe language. Because Haxe Macros allow you to run some code at compile-time that will generate a Haxe program, the possibilities are unlimited.
Let's quote a few possible usages :
- store the compilation Date into your generated code/binary
- include in your generated code/binary any data that is stored into external files
- perform compile-time encryption
- check SQL, XML, or any other syntaxes at compile-time (with precise error display)
- implement DSLs that will get compiled natively
- rebind the semantics of the language in order to implement new features such as "With" statement, Monads, etc.
- .... and much more !
One of the nice things with Haxe Macros is that they do NOT modify the syntax of the language. The Haxe code continue to be perfectly readable for any Haxe (or Java/JS/C++/AS3/etc.) developer.
Want to learn more about it ? Check Macros documentation
PS : using macros require the latest Haxe SVN, check this page for build instructions.
Wow, a very advanced feature. I really like the implementation and the general idea behind it.
From a technological point of view I guess you run the Macro in Neko to generate part of the AST? Really smart way to hook it into the compiler. I just wonder how and when you perform semantic analysis in this case.
Great new feature! Thank you!
@Joa : the AST is sent to the macro as-it and the macro returns an AST, which is then compiled by Haxe. So you can use any expression as macro parameter as long as it's syntacticly compatible with Haxe.
In order to run the macro, I'm generating on-the-fly the Neko AST from the Haxe AST, then interpreting it using a NekoVM emulator embedded into the Haxe compiler. Results show very good performances so far.
Wow ! Haxe rocks !
Have you considered also targeting D? It's quite similar to C++, but its compilation speed is incredible.
@Lachlan
I'd much rather have a D to Haxe compiler :-)
This looks like a great feature, I'll have to check it out in detail soon!
I don't think we need these sugars too much , but we just waiting flash player 10.1 APIs for a long time .
Yes, macros finally! Now, it's no excuse to not do up that long-overdue log2() macro when configuring static inline builds (for things like default texture/memory sizes, etc.)
We are celebrating macros with champagne and whores here, thanks Nicolas! I was dreaming about macro in haxe since I met and fell inlove with this lovely "Esperanto come true" years ago.
Anyway I got lots of questions, a first bunch is posted on forum.
@Icebird, never call this a sugar, it was my precious dream for a long time (by the way, I love sugar, at least 3 spoons per a cup of tea :) )
Great! The haxe 2.06 don't contain macros, how can get the latest compiler with macros.
I really wishes I could share your enthusiasms but I can't because
I can understand what make Macro so great.
Could anybody write a simple tutorial or
could you also update the haxe languages reference guide please :
It is found here with an example on our to use these macro.
http://github.com/sagework/Haxe_lang_ref/raw/master/Haxe2_lang_ref_sep_24_2010.pdf
Ah it is not in 2.06... well I guess I will have to wait than.
Well I guess,
Great work Nicolas and
Long life to HAXE!!!
current nightly build verison Macro feature not support windows 7,I did a test
Very cool. Haxe only needs a few more targets and it can take over the world :). I'd like to see JVM, .NET/CLR/CLI, LLVM/native + maybe C.
Excellent work Nicholas. I'll be digging into it in the coming weeks!
Hey Mr. Nicolas Cannasse,
The website http://haxe.org/ just went down, and I need it :p , I'm not that fluent in the API - so I need an reference.
And now it is up and running again :D
I've been able to solve some interesting problems with macros; however, I have been unable to decode the syntax required to write a new function at compile time.
- How would you write a function like this? Presumably with EFunction?
static function foo( d1 : Dynamic ) : Dynamic {
return {bar:1};
}
- How can I create a Position (that targets a different class), since the function should not exist in the position from which the macro is called (and returned by Context.currentPos)?
-Thanks-
@Brett : macros cannot generate new static/member methods so far, they can only generate an expression, which is contained into an expression.
You might however be able to generate a static function, by using :
static var foo = MyMacro.generate()
Positions can now be accessed/created by using Context.getPosInfos/makePosition