Created
December 29, 2016 23:00
-
-
Save binki/c30c6eebf9c5345e7e744bb1cd805ae8 to your computer and use it in GitHub Desktop.
Haxe: investigating string interpolation and macros
This file contains hidden or 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
// Haxe’s macro support makes for a golden opportunity for building | |
// program metadata during compiletime. I think it would be very cool | |
// to be able to preprocess itnerpolated strings so that text can be | |
// extracted and analyzed separately (NLS?) or, e.g., to do JavaScript-style | |
// tagged string stuff/C#-style FormattableString stuff like automatically | |
// escaping expressions when building HTML, etc. | |
// | |
// However, it looks to me like right now macros are given CStrings | |
// which don’t even let you know if the string will be interpolated | |
// or not. | |
import haxe.macro.Expr; | |
class InterpolatedMacroPlay { | |
static function main() { | |
trace(intr('asdf')); | |
trace(intr('asdf${2*3}')); | |
trace(intr('asdf${{}}asdf')); | |
trace(intr("as${2*3}df")); | |
} | |
public macro static function intr(e:Expr) { | |
switch (e.expr) { | |
case ExprDef.EConst(c): | |
switch (c) { | |
case CString(s): trace('str: ${s}'); | |
default: | |
} | |
case ExprDef.EBinop(op, e1, e2): | |
trace('binop: ${e1} ${op} ${e2}'); | |
default: | |
} | |
return e; | |
} | |
} |
This file contains hidden or 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
$ haxe --run InterpolatedMacroPlay | |
InterpolatedMacroPlay.hx:26: str: asdf | |
InterpolatedMacroPlay.hx:26: str: asdf${2*3} | |
InterpolatedMacroPlay.hx:26: str: asdf${{}}asdf | |
InterpolatedMacroPlay.hx:26: str: as${2*3}df | |
InterpolatedMacroPlay.hx:16: asdf | |
InterpolatedMacroPlay.hx:17: asdf6 | |
InterpolatedMacroPlay.hx:18: asdf{}asdf | |
InterpolatedMacroPlay.hx:19: as${2*3}df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment