Macro, first try

This small tutorial is about "macros", and I used haXe 2.07.

If you want to learn more about macros with haXe I just recommend you to read Nicolas's post on his blog.

Possibilities of use are unilimited...so this is my first try with macros made for beginners. It's very simple, we have two source files, Main.hx, and MyCustomMacro.hx.

Our custom macro is going to create on the fly a new class (MyCircle) at compile-time, and we're going to instanciate a new one in our main class.

Ok, let's start with our custom macro :

package ; 
/**
 * ...
 * @author razaina
 */

 //These are preprocessor macros
 //(learn more about Conditional Compilation here > http://haxe.org/ref/conditionals)
#if macro  
import haxe.macro.Context;
import haxe.macro.Type;
import haxe.macro.Expr;
import neko.FileSystem;
import neko.io.File;
#end

class MyCustomMacro
{
	//we define our function createClass as a macro function by using @:macro metadata
	// learn more about metadatas here > http://haxe.org/manual/metadata
	@:macro public static function createClass()
	{
		//Create a haXe source file with the same name of your class name.
		var file = File.write("MyCircle.hx", false);

		//Now create your class by writing it directly into the file 
		file.writeString('  
			import flash.display.Sprite;
			
			class MyCircle extends Sprite
			{
				public function new()
				{
					super();

					//Here we just draw a simple black circle
					this.graphics.beginFill(0x000000);
					this.graphics.lineStyle(1, 0x000000); 
					this.graphics.drawCircle(100, 100, 40); 
				}

				//This static function is very important, \'cause it returns a new instantitation of our class
				public static function getCircle()
				{
					return new MyCircle();
				}

				//You can define methods....
				//don\'t forget to backslash simple quotes
				public function doSomething()
				{
					trace("Hey, I\'ve been created on the fly :) ! haXe just kick ass !");
				} 
			}
		');

		//You can close your file now
		file.close();

		//At compile-time the file is created, parsed, and MyCircle is going to be typed
		//Then you can get it
		Context.getType("MyCircle");

		//You can delete the file
		FileSystem.deleteFile("MyCircle.hx"); 

		//And finally your macro function must always return an expression
		//which correspond to the block of code that will replace the macro call 
		return { expr:EConst(CType("MyCircle")), pos:Context.currentPos() };
	}
}

We just have to create our main class now :

package ; 
 
import flash.Lib; 

/**
 * ...
 * @author razaina
 */ 

 //Main Class
class Main 
{ 
	static function main() 
	{
		//Use your macro to create your new Class then get it
		var myCircle = MyCustomMacro.createClass().getCircle();
		Lib.current.stage.addChild(myCircle);
		
		//Then you can call any public method 
		myCircle.doSomething();
	} 
	public function new()
	{
		new Main();
	}
} 

Compile then test it !

haxe -main Main -swf9 TestMacro01.swf

output

Alternative content

Get Adobe Flash player

(12 times)