Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created June 14, 2016 00:06
Show Gist options
  • Save francescoagati/a1f4852ecc6b791128335541f722a279 to your computer and use it in GitHub Desktop.
Save francescoagati/a1f4852ecc6b791128335541f722a279 to your computer and use it in GitHub Desktop.
haxe abstract and inline objects
class Getter<T> {
var name:String;
var obj:T;
public inline function new(name:String,obj:T) {
this.name = name;
this.obj = obj;
}
public inline function get() return untyped (obj:haxe.DynamicAccess<Dynamic>)[name];
}
abstract Resolver(Dynamic) from Dynamic to Dynamic {
@:resolve public inline function resolver(name:String) {
return new Getter(name,this);
}
}
class Main {
static function main() {
var wrapper:Resolver = untyped {a:1};
trace(wrapper.a.get());
}
}
// Generated by Haxe 3.3.0
(function () { "use strict";
var Main = function() { };
Main.main = function() {
var wrapper = { a : 1};
var _this_name = "a";
var _this_obj = wrapper;
console.log(_this_obj[_this_name]);
};
Main.main();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment