new A().b() currently requires extra brackets/parentheses

Ben Davis entheh at cantab.net
Sun Sep 9 05:49:01 PDT 2012


Hi,

I'm trying to define an API in D where an object can be instantiated and 
then set up inline. I have various methods that return 'this' so that 
they can be used inline in this way. For example:

dialogue=new GuiDialogue([
	(new GuiLabel(titleText)).setLayoutHint(GuiLayoutHint.TOP),
	fileNameInput.setLayoutHint(GuiLayoutHint.TOP),
	currentDirectoryLabel.setLayoutHint(GuiLayoutHint.TOP),
	confirmButton,
	new GuiButton("Cancel",delegate {dialogue=null;}),
]);

D's syntax currently requires me to put the new A() part in brackets. So 
instead of

new A().b()

I have to write

(new A()).b()

If I don't do that, then the error is "found . when expecting ," - which 
sounds as if it's already decided it's finished reading the expression, 
so there isn't a problem with ambiguity. (I suppose if the brackets are 
optional, then "new A.b()" would be ambiguous, but this is no different 
from the 'else' binding problem.)

So - is this something that could be relaxed?

Thanks,

Ben :)


More information about the Digitalmars-d mailing list