Fastest way to check using if identifier has already been defined, using static if or similar?
Q. Schroll
qs.il.paperinik at gmail.com
Thu Jun 4 14:20:45 UTC 2020
On Thursday, 4 June 2020 at 09:03:40 UTC, Simen Kjærås wrote:
> string exists(string s) {
> return "__traits(compiles, { mixin(\"alias _ = "~s~";\");
> })";
> }
Little nitpicking, but D has many forms of string literals.
Escaping is hard to read especially without syntax highlighting.
Escaping to me is an old hack C did we shouldn't really use.
string exists(string s) {
return `__traits(compiles, { mixin("alias _ = ` ~ s ~
`;"); })`;
}
The _ as a name, to me, proves that a __traits(freshName), that
returns a string that is distinct from every symbol name visible
from the point it's defined, would be useful in these occasions.
Because if someone used _ as an identifier in a context where the
`exisits` function is used, it might fail.
More information about the Digitalmars-d-learn
mailing list