Postfix type notation - readability and parsing?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Mar 7 21:06:28 UTC 2019


On Thu, Mar 07, 2019 at 08:22:36PM +0000, Paul Backus via Digitalmars-d wrote:
> On Thursday, 7 March 2019 at 19:43:31 UTC, aliak wrote:
> > Thanks for that code above!
> > 
> > And hmm.. __GENSYM__ ... is this documented anywhere? (I found a
> > post by you on the forum about it but nothing in the docs)
> 
> `gensym` is a function in some Lisp-family languages that's used to
> generate unique ientifiers for use in macros. [1] It sounds like
> __GENSYM__ is a proposed version of this for D, implemented as a
> compiler builtin like __FILE__ and __LINE__.
> 
> [1] http://clhs.lisp.se/Body/f_gensym.htm

I've found the need for generating unique identifiers before, especially
for temporaries inside `static foreach`.  One day, it dawned on me that
templates can be be used to generate new names, precisely because each
template instantiation is given a unique identifier based on the
template arguments:

	struct S(size_t i) {
		... // declarations that depend on i
	}

	static foreach (i; 0 .. 10) {{
		S!i x; // <-- bingo, new type identifier per loop index!
	}}

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

-- 
What are you when you run out of Monet? Baroque.


More information about the Digitalmars-d mailing list