Skip to content

Instantly share code, notes, and snippets.

@Simn
Simn / XmlParser.hx
Created April 19, 2012 22:53
Port of neko C Xml parser to haxe
package haxe.xml;
using StringTools;
enum State
{
IGNORE_SPACES;
BEGIN;
BEGIN_NODE;
TAG_NAME;
@Simn
Simn / Main.hx
Created October 16, 2012 10:52
Haxe macro for array of char codes
import haxe.macro.Expr;
class Main {
static function main() {
var a = generateCharCodeArray("foobar");
trace(a); // [102,111,111,98,97,114]
}
#if haxe_211
@:macro static public function generateCharCodeArray(source:String) {
@Simn
Simn / Main.hx
Created November 3, 2012 16:59
Function-based specialization wizardry in haxe3
class Main {
static function main() {
trace(process(12)); // 24
trace(process("Hello world")); // HELLO WORLD
}
@:generic static function process<T>(t:T):T {
return throw "unknown type: " + t;
}
@Simn
Simn / Main.hx
Last active December 10, 2015 06:59
haxe @:font example
@:font("C:/Windows/fonts/Arial.ttf", "a-zA-Z {}\\-\"1234")
class MyFont extends flash.text.Font { }
class Main {
static function main() {
var font = new MyFont();
var fmt = new flash.text.TextFormat();
fmt.size = 50;
fmt.font = font.fontName;
@Simn
Simn / Main.hx
Last active February 22, 2021 09:51
New reification examples
class Main {
static function main() {
MyMacro.testReify();
}
}
@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);
@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 / 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 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 / 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();