new X().bar(); is a syntax error?

Robert Fraser fraserofthenight at gmail.com
Thu Sep 20 20:07:04 PDT 2007


I think it might create a parsing ambiguity. Since you can refer to inner clases by prefixing them with the name of the object or type, there's no way to tell during the syntactic pass whether it's a qualified type name or a call expression. You can do it in java/C# because you can't omit the trailing parentheses at the end of the new expression, so once it hits a closing parenthesis, the parser knows that's the end of the new expression.

Ary Manzana Wrote:

> BCS escribió:
> > Reply to Ary,
> > 
> >> Compiling:
> >>
> >> ---
> >> class X {
> >> void bar() {
> >> }
> >> }
> >> void foo() {
> >> new X().bar();
> >> }
> >> ---
> >>
> >> gives me:
> >> main.d(7): found '.' when expecting ';' following 'statement'
> >> Is this a bug or is it the intended behaviour? I can do this in Java,
> >> C#, probably C++...
> >>
> > 
> > I think it's not parsing how you want it, try this:
> > 
> > (new X()).bar();
> 
> But I can do it without parenthesis in other languages... I'll try to 
> fix this in Descent's parser and submit a bug report.



More information about the Digitalmars-d-learn mailing list