Fastest way to check using if identifier has already been defined, using static if or similar?

Paul Backus snarwin at gmail.com
Thu Jun 4 15:04:46 UTC 2020


On Thursday, 4 June 2020 at 14:20:45 UTC, Q. Schroll wrote:
>
> 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.

In the Lisp language family, this feature is called "gensym" 
(generate symbol). It is indeed quite useful to have when 
generating code.

I believe it was proposed at one point to add a special 
__GENSYM__ keyword, similar to the current __FILE__, __LINE__, 
__MODULE__, etc. that would evaluate to a unique identifier each 
time it was used. I'm not sure what became of that proposal. 
Maybe it needed a DIP and no one was available to write one?


More information about the Digitalmars-d-learn mailing list