Last active
February 22, 2021 09:51
-
-
Save Simn/4439850 to your computer and use it in GitHub Desktop.
New reification examples
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() { | |
MyMacro.testReify(); | |
} | |
} |
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; | |
using haxe.macro.ExprTools; | |
class MyMacro { | |
macro static public function testReify() { | |
// ==== $i{string} ==== | |
// Using $i{string} generates an EConst(CIdent(string)) | |
print(macro $i{"foo"}); // foo | |
// Any code can be used within the { } as long as it yields a String | |
print(macro $i{"foo".toUpperCase()}); // FOO | |
// ==== $e{expr} ==== | |
// Both $e{code} and ${code} can be used to execute arbitrary code, as | |
// long as it returns an Expr | |
print(macro $e{getExpr()}); // "bar" | |
print(macro ${getExpr()}); // "bar" | |
// This can also be nested | |
print(macro ${macro 1} ); // 1 | |
// ==== $v{dynamic} ==== | |
// This does what $(dynamic) used to do: It creates an expression from | |
// a given value. | |
print(macro $v{1} ); // 1 | |
print(macro $v{"foo"} ); // "foo" | |
// Again, any code can be executed within the { } | |
print(macro $v { getString() } ); // "myString" | |
// ==== $a{array} ==== | |
// This does what $[array] used to do: It expects an Array<Expr> and can | |
// be used in places that do so too: | |
var myArguments = [macro 1, macro 2]; // Array<Expr> | |
function myFunction(a, b) return a + b; // Int -> Int -> Int | |
print(macro myFunction($a { myArguments } )); // myFunction(1,2) | |
// This includes blocks | |
print(macro { | |
$a{myArguments} | |
}); // { 1; 2; } | |
// Note that this means something else | |
print(macro { | |
$a { myArguments }; | |
$a { myArguments }; | |
}); // { [1,2]; [1,2]; } | |
// If you don't happen to have an Array<Expr> around, you can create it | |
// within the { } | |
print(macro $a{[$v{1}, $v{2}]}); | |
// Here are all four modifier combined | |
print(macro $a{[$v{1}, $i{"foo"}, $e{getExpr()}]}); // [1,foo,"bar"] | |
// ==== var $name ==== | |
// String variables can be used to generate a variable name like so: | |
var name = "foo"; | |
print(macro var $name = 1); // var foo = 1 | |
// ==== expr.$name ==== | |
// In a similar fashion, field access can be generated: | |
var funcName = "charAt"; | |
print(macro "bar".$funcName(1)); // "bar".charAt(1) | |
// This can also be chained | |
print(macro "bar".$funcName(1).$funcName(1)); // "bar".charAt(1).charAt(1) | |
return macro null; | |
} | |
static function getExpr() { | |
return macro "bar"; | |
} | |
static function getString() { | |
return "myString"; | |
} | |
static function print(e:Expr) trace(e.toString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment