Created
August 15, 2016 14:12
-
-
Save francescoagati/62649442a15640a0e6475638e32a0a51 to your computer and use it in GitHub Desktop.
haxe 3.3 handling events with abstracts resolve macro and inlining objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import haxe.macro.Expr; | |
#if macro | |
import haxe.macro.Context; | |
using tink.MacroApi; | |
using haxe.macro.ExprTools; | |
using haxe.macro.Context; | |
#end | |
#if !macro | |
class Wrapper { | |
public var property:String; | |
public var obj:js.html.Element; | |
public inline function new(property:String,obj:js.html.Element) { | |
this.property = property; | |
this.obj =obj; | |
} | |
public inline function handle(fn:Dynamic->Void) obj.addEventListener(property,fn); | |
} | |
#end | |
abstract EventHandler(Dynamic) from Dynamic to Dynamic { | |
@:resolve public static macro function resolve(self:Dynamic,name:ExprOf<String>) { | |
return macro new EventHandler.Wrapper($name,$self); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Main { | |
static function main() { | |
var x:EventHandler = js.Browser.document.createElement('div'); | |
x.click.handle(function(e) {}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by Haxe 3.3.0 | |
(function () { "use strict"; | |
var Main = function() { }; | |
Main.main = function() { | |
var x = window.document.createElement("div"); | |
var _this_property = "click"; | |
var _this_obj = x; | |
_this_obj.addEventListener(_this_property,function(e) { | |
}); | |
}; | |
Main.main(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment