Skip to content

Instantly share code, notes, and snippets.

@back2dos
back2dos / Main.hx
Created September 19, 2012 06:42
Statically typed metadata (compile with -lib tink_macros --macro TagChecker.use())
package ;
import haxe.rtti.Meta;
@:tag('foo') typedef Foo = {
?bar:String,
?baz:Int
}
@:tag typedef Bar = Dynamic;
@back2dos
back2dos / Optional.hx
Created October 20, 2012 08:35
Optional interface fields with macros
package ;
import tink.macro.build.Member;
import tink.macro.build.MemberTransformer;
import haxe.macro.Type;
using tink.macro.tools.MacroTools;
using tink.core.types.Outcome;
class Optional {
@back2dos
back2dos / Main.hx
Created October 26, 2012 13:35
Example of partial implementations
package ;
import tink.lang.Cls;
class Main {
static function main() {
var foods = 'cookie,soup,banana'.split(',');
var eaters = [new Human(), new CookieMonster(), new Robot()];
for (e in eaters)
for (f in foods)
e.eat(f);
@back2dos
back2dos / Main.hx
Last active December 10, 2015 22:28
Hello world with tinx_node
package ;
class Main implements tink.lang.Cls {
static function main()
@with(tinx.node.Http.server(2000))
@on(request)
request.respond().end('hello world')
}
@back2dos
back2dos / Main.hx
Created January 22, 2013 15:25
Metadata checking
package ;
@:build(MetaCheck.build())
class Main {
static function main() {
trace(Meta.getStatics(Main).main.AngularSupport);
}
@AngularSupport({ inject:["$scope", '$http'], scope:'$scope' })
function foo() {}
@AngularSupport({ inject:["$scope", '$http'], scfope:'$scope' })//will cause an error
@back2dos
back2dos / Main.hx
Created February 6, 2013 00:00
Flon's Law at work.
class A {
public var i(get, null):Int;
public function new() i = 0
function get_i() return i--
}
class Main {
static function main()
switch (new A()) {
case a if (a.i < 0): trace('negative');
case a if (a.i == 0): trace('zero');
package ;
abstract JsonMap<T>({ }) from {} {
public function new() this = {};
public function exists(key:String) return Reflect.hasField(this, key);
@:arrayAccess public function get(key:String) return Reflect.field(this, key);
@:arrayAccess public function set(key:String, value:T):T {
Reflect.setField(this, key, value);
return value;
}
import haxe.macro.Expr;
import haxe.macro.Context;
class Build {
static public function types() {
var pos = Context.currentPos();
function mkPath(name:String):TypePath {
var parts = name.split('.');
return {
sub: null,
@back2dos
back2dos / Interp.hx
Last active December 15, 2015 16:49
Getting constant values of expressions at macro time.
package ;
import haxe.macro.Expr;
import haxe.macro.Context;
class Interp {
static function error(pos:Position, ?msg = 'Constant value expected'):Dynamic
return Context.error(msg, pos);
static function const(c:Constant, pos):Dynamic
return
@back2dos
back2dos / Example.hx
Last active December 16, 2015 03:48
Inference issue.
package ;
abstract Abstract<T>(T->Void) from (T->Void) {
function new(f:T->Void)
this = f;
}
class Example {
static function testAbstract(c:Abstract<haxe.ds.Option<Bool>>) { }
static function testFunction(c:haxe.ds.Option<Bool>->Void) { }