Postfix type notation - readability and parsing?

Paul Backus snarwin at gmail.com
Sat Mar 9 20:50:37 UTC 2019


On Thursday, 7 March 2019 at 21:06:28 UTC, H. S. Teoh wrote:
> Unfortunately, this does not solve the problem of unique 
> identifiers for the variable name (as opposed to the type 
> name). But perhaps the same idea of using templates to generate 
> identifiers might still be applicable:
>
> 	// Warning: untested concept code
> 	alias gensym(size_t line = __LINE__) = Option!(int, "", "", 
> "", 0);
>
> Or something along these lines, might be the ticket?  
> Essentially, generate a new identifier keyed on __LINE__ (you 
> could also add __FILE__ if you wish to differentiate between 
> declarations across source files). So the identifiers would be 
> gensym!123, gensym!456, etc..
>
>
> T

The only issue with using __LINE__ is that it requires gensym to 
be evaluated in the same scope as the one the variable is being 
declared in. For example, if you have a function that generates 
code as a string for use as a mixin, you can't use a 
__LINE__-based gensym to name symbols in the generated code:

import std.format;

string gensym(size_t id = __LINE__) {
     return format("_gensym_%d", id);
}

string generateCode() {
     enum varName = gensym;
     return q{
         import std.stdio;
         int %1$s = 123;
         writeln("%1$s = ", %1$s);
     }.format(varName);
}

void main() {
     mixin(generateCode);
     //mixin(generateCode); // Error: _gensym_8 is already defined
}

In Lisp, gensym uses a global counter (like GCC's __COUNTER__ 
macro [1]), so this isn't an issue.

[1] 
https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html


More information about the Digitalmars-d mailing list