Skip to content

Instantly share code, notes, and snippets.

@back2dos
back2dos / Animals.hx
Created July 27, 2013 09:37
Fun with abstracts: grouping members without overhead.
package ;
abstract Animals(ExampleData) from ExampleData {
public var dog(get, never):Animal;
inline function get_dog():Animal return this.dog;
public var cat(get, never):Animal;
inline function get_cat():Animal return this.cat;
public var cow(get, never):Animal;
@back2dos
back2dos / Build.hx
Last active December 19, 2015 13:48
Store build information in metadata.
package ;
import haxe.rtti.Meta;
typedef BuildInfo = {
date: Date,
}
class Build {
static public var info(get, null):BuildInfo;
static function get_info() {
@back2dos
back2dos / Main.hx
Created May 1, 2013 16:09
ECheckType and implicit conversion issue, haxe r6518
package ;
abstract MyAbstract(Int) {
public function new(v:Int) this = v;
@:from static function fromString(s:String) return new MyAbstract(Std.parseInt(s));
}
class Main {
#if !macro
static function main() {
var d:Dynamic = test();
@back2dos
back2dos / Manual.hx
Created April 25, 2013 15:49
Manually manage memory with a pool.
package ;
class Manual {
static var pool = [];
static var check = new Map<Manual, Manual>();
var count:Int = 0;
function new() {}
function init() {/*set up*/}
function free() {/*clean up*/}
@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) { }
@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
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,
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;
}
@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');
@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