February 2021
Mo Tu We Th Fr Sa Su
<< >>


Haxe got Macros !

Posted on Sep 11 2010

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.

  • Sep 11, 2010 at 16:12

    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.

  • Ivan
    Sep 11, 2010 at 19:25

    Great new feature! Thank you!

  • Sep 11, 2010 at 22:46

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

  • thienhaflash
    Sep 12, 2010 at 08:54

    Wow ! Haxe rocks !

  • Lachlan Stuart
    Sep 12, 2010 at 12:14

    Have you considered also targeting D? It's quite similar to C++, but its compilation speed is incredible.

  • Danny Wilson
    Sep 13, 2010 at 02:10


    I'd much rather have a D to Haxe compiler :-)

  • ben
    Sep 14, 2010 at 10:58

    This looks like a great feature, I'll have to check it out in detail soon!

  • Icebird
    Sep 15, 2010 at 09:37

    I don't think we need these sugars too much , but we just waiting flash player 10.1 APIs for a long time .

  • Sep 17, 2010 at 19:28

    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.)

  • Shal
    Sep 24, 2010 at 07:57

    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 :) )

  • Oct 13, 2010 at 04:24

    Great! The haxe 2.06 don't contain macros, how can get the latest compiler with macros.

  • pixnlove
    Oct 17, 2010 at 20:16

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

  • layola
    Oct 23, 2010 at 16:37

    current nightly build verison Macro feature not support windows 7,I did a test

  • Nov 08, 2010 at 14:22

    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!

  • Dec 11, 2010 at 10:32

    Hey Mr. Nicolas Cannasse,

    The website just went down, and I need it :p , I'm not that fluent in the API - so I need an reference.

  • Dec 11, 2010 at 12:28

    And now it is up and running again :D

  • Jan 14, 2011 at 22:10

    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)?


  • Jan 15, 2011 at 11:21

    @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

Name : Email : Website : Message :