Skip to content

Instantly share code, notes, and snippets.

@Simn
Simn / Fast.hx
Created March 14, 2014 09:25
Haxe abstract + macro Xml fun
import haxe.macro.Expr;
using haxe.macro.Tools;
abstract Fast(Xml) from Xml to Xml {
@:noCompletion public inline function new(xml:Xml) this = xml;
public var name(get,never):String;
inline function get_name() return this.nodeName;
public var elements(get,never):FastIterator;
@Simn
Simn / LexerGraph.hx
Created March 12, 2014 16:13
LexerGraph for hxparse
import hxparse.Ruleset;
import hxparse.State;
import dot.Graph;
import dot.Node;
import dot.Attribute;
using Lambda;
class LexerGraph<T> {
var graph:Graph;
@Simn
Simn / DeprecationMacro.hx
Last active August 29, 2015 13:57
Haxe @:deprecated test
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
using haxe.macro.Tools;
using Lambda;
class DeprecationMacro {
/**
@Simn
Simn / Main.hx
Created March 5, 2014 12:08
@:generic map copy
class Main {
function new() { }
static function main() {
var intMap = [1 => "foo", 2 => "bar"];
var stringMap = ["foo" => 1, "bar" => 2];
var m1 = new Main();
var m2 = new Main();
var objMap = [m1 => "foo", m2 => "bar"];
@Simn
Simn / Main.hx
Created February 19, 2014 18:29
initLocals macro
class Main {
var arg1:Int;
var arg2:String;
var arg3:Bool;
function new(arg1 = 0, arg2 = "foo", arg3 = false) {
MyMacro.initLocals();
}
@Simn
Simn / Main.hx
Created August 2, 2013 08:32
Haxe generic type parameter construction
typedef Constructible = {
public function new():Void;
}
@:generic
class Gen<T:(Constructible, Main)> {
public function new() { }
public function make() {
return new T();
@Simn
Simn / Main.hx
Created May 7, 2013 18:49
printf for haxe using GADT and macros
class Main {
static public function main () {
var v = Printf.sprintf("The sum of $i and $i is $s.", 3, 5, "8");
trace(v); // The sum of 3 and 5 is 8.
}
}
@Simn
Simn / AbstractBuilder.hx
Created February 22, 2013 07:48
Haxe abstract builder example
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
class AbstractBuilder {
macro static public function build():Array<Field> {
var fields = Context.getBuildFields();
var cCur = Context.getLocalClass().get();
var fieldMap = [for (f in fields) f.name => true];
function loop(c:ClassType) {
@Simn
Simn / Main.hx
Created January 29, 2013 10:02
Dynamic implementation switch with abstracts
interface I {
public function print(s:String):Void;
}
class C1 implements I {
public function new() { }
public function print(s) trace('C1: $s')
}
class C2 implements I {
@Simn
Simn / Builder.hx
Last active December 11, 2015 07:09
Build + reification example
import haxe.macro.Context;
import haxe.macro.Expr;
class Builder {
macro static function build():Array<Field> {
var funcExpr = macro function():String {
return "test";
}
var ctorExpr = macro function(foo) {
trace("I was created with " +foo);