Template instantiation syntax

Ary Borenszweig ary at esperanto.org.ar
Sun Oct 12 09:04:00 PDT 2008


Dave escribió:
> 
> "Walter Bright" <newshound1 at digitalmars.com> wrote in message 
> news:gcogl4$28ui$1 at digitalmars.com...
>> We seem to have reached a dead end on finding a significantly better 
>> alternative than foo!(bar).
>>
>> All is not lost, though. Andrei is working on an emacs module that 
>> will parse D code and replace foo!(bar) with foo«bar» for display only 
>> when the editor is in D mode, the underlying text will still be 
>> foo!(bar). (This doesn't affect D at all, only its display in Emacs.)
>>
>> Also, we're going to try using ! for single argument syntax, as in:
>>
>> foo!bar  is same as   foo!(bar)
>> foo!10   is same as   foo!(10)
>>
>> etc. 0 arguments or more than 1 argument or arguments that are more 
>> than one token long will still require !( ). We'll see how that works. 
>> I think it looks rather nice.
> 
> I don't like it because it is not consistent and therefore could make 
> things ugly and even harder to comprehend where clarity is needed most 
> -- in a file full of mixed length template instantiations. It reeks of 
> total hack to me, and I think this is opening a huge can of worms 
> regarding the a!b!c issue. Inconsistency and things that smack of 
> potential "corner case" are never good.
> 
> I hadn't seen this mentioned lately; C#, Java and now C++0x are able to 
> work around the '> >' issue and allow '>>'.

I think that in Java and C# it's a lot easier than in D or C++, mainly 
because Java and C# allow only types as generic arguments.

---
Type:
         Identifier [TypeArguments]{   .   Identifier [TypeArguments]} {[]}
         BasicType

TypeArguments:
         < TypeArgument {, TypeArgument} >

TypeArgument:
         Type
         ? [( extends |super ) Type]
---

So in your parser, if you encounter XXX<YYY<ZZZ>>, that last >> never 
means right shift, since ZZZ >> (something) would form an expression, 
not a type. So the parser can say:

if after ZZZ comes >, it closes the < of YYY. If >> comes, and we are in 
a nested generic, then close both < of YYY and < of XXX.

I debugged JDT's parser and here it is:

---
// when consuming a rule...
case 538 : if (DEBUG) { System.out.println("ReferenceType2 ::= 
ReferenceType RIGHT_SHIFT"); }  //$NON-NLS-1$
		    consumeReferenceType2();
			break;
case 544 : if (DEBUG) { System.out.println("ReferenceType3 ::= 
ReferenceType UNSIGNED_RIGHT_SHIFT"); }  //$NON-NLS-1$
		    consumeReferenceType3();
			break;
---

So also >>> is hard, but not that much.

Of course, that's harder than just having other symbols for <>, because 
you have to maintain a stack of generics so far, but it's not that hard.



More information about the Digitalmars-d mailing list