Last active
November 14, 2023 14:12
-
-
Save pbackus/2fde8eb2a6eba15905daba65256f33aa to your computer and use it in GitHub Desktop.
Gensym for D
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
// Mixin to generate a new identifier that won't repeat within a scope | |
enum gensym(string prefix = "_gensym") = | |
`"` ~ prefix ~ `" ~ __traits(identifier, {})["__lambda".length .. $]`; | |
// Works multiple times on the same line | |
unittest | |
{ | |
enum sym1 = mixin(gensym!()); enum sym2 = mixin(gensym!()); | |
static assert(sym1 != sym2); | |
mixin("int ", sym1, " = 123, ", sym2, " = 456;"); | |
assert(mixin(sym1) == 123); | |
assert(mixin(sym2) == 456); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment