Skip to content

Instantly share code, notes, and snippets.

@skial
Created August 18, 2016 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skial/592a9c1e58ff701e186de19fae31e946 to your computer and use it in GitHub Desktop.
Save skial/592a9c1e58ff701e186de19fae31e946 to your computer and use it in GitHub Desktop.
Sample code for abstract macro question over on the Haxe mailing list - https://groups.google.com/d/msg/haxelang/0IBfsEs3OHg/Hx9DfmDTAwAJ
-cp src
-debug
-main Main
--each
-js bin/hasString.js
// Generated by Haxe 3.3.0 (git build development @ 6163882)
(function () { "use strict";
var Main = function() {
console.log(["a","c","b"].toString());
};
Main.main = function() {
new Main();
};
Main.main();
})();
//# sourceMappingURL=hasString.js.map
{
"version":3,
"file":"hasString.js",
"sourceRoot":"file:///",
"sources":["C:/dev/macro_abstractHasString/src/Main.hx","C:/dev/macro_abstractHasString/src/MyAbstract.hx"],
"names":[],
"mappings":";;WAQQ,WACN;AAAA,aC0Be,CA/BD,AAAC,IACD,AAAC,IACD,AAAC;;YDFF,WACb;AAAA;;;;;"
}
package ;
class Main {
public static function main() {
new Main();
}
public function new() {
trace( MyAbstract.getAllValues() );
}
}
package ;
@:enum abstract MyAbstract(String) from String to String {
public var a = 'a';
public var b = 'c';
public var c = 'b';
public static macro function getAllValues() {
var self = haxe.macro.Context.getType('MyAbstract');
var results = [];
switch self {
case TAbstract(_.get() => ab, params):
/*trace( ab );
trace( ab.impl.get() );
trace( ab.impl.get().fields.get() );*/
for (field in ab.impl.get().statics.get()) {
switch field.kind {
case FVar(_, _):
try {
var expr = haxe.macro.Context.getTypedExpr( field.expr() );
if (expr != null) results.push( expr );
} catch (e:Dynamic) {
}
case _:
}
};
case _:
}
return macro $a{results};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment