Jump to page: 1 2
Thread overview
new A().b() currently requires extra brackets/parentheses
Sep 09, 2012
Ben Davis
Sep 09, 2012
Philippe Sigaud
Sep 09, 2012
Ben Davis
Sep 09, 2012
Nick Treleaven
Sep 09, 2012
Jacob Carlborg
Sep 09, 2012
Philippe Sigaud
Sep 10, 2012
Nick Treleaven
Sep 10, 2012
Philippe Sigaud
Sep 09, 2012
Timon Gehr
Sep 09, 2012
Ben Davis
Sep 09, 2012
Timon Gehr
Sep 09, 2012
Timon Gehr
Sep 10, 2012
Sönke Ludwig
Sep 10, 2012
Timon Gehr
Sep 10, 2012
Sönke Ludwig
Sep 10, 2012
Andrej Mitrovic
September 09, 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 :)
September 09, 2012
On Sun, Sep 9, 2012 at 2:49 PM, Ben Davis <entheh@cantab.net> wrote:

> 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()

Hi Ben,

you could use factory functions:

class A {}

A a() { return new A();}

(and appropriate overloads for the different A constructors, of
course). Which would give you:

dialogue=guiDialogue([
        guiLabel(titleText).setLayoutHint(GuiLayoutHint.TOP),
        fileNameInput.setLayoutHint(GuiLayoutHint.TOP),
        currentDirectoryLabel.setLayoutHint(GuiLayoutHint.TOP),
        confirmButton,
        guiButton("Cancel", {dialogue=null;}),
]);

For the delegate at the end, I *guess* (but I'm not certain) that just
using { ... } will work. It's a void delegate().


Then:
September 09, 2012
On 09/09/2012 02:49 PM, Ben Davis wrote:
> 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 :)

I think so. Probably it is just a bug. (the spec does not indicate that
it is illegal iirc.) Maybe you can even fix it yourself, the parser
should be quite hackable.
September 09, 2012
Hi,

On 09/09/2012 14:23, Philippe Sigaud wrote:
> you could use factory functions:
>
> class A {}
>
> A a() { return new A();}

Good idea. It might not work so well for me because the user might also be creating custom GUI components and would therefore have to create their own factory functions, but thanks for the idea in any case.

> For the delegate at the end, I *guess* (but I'm not certain) that just
> using { ... } will work. It's a void delegate().

You're right - it does compile without the keyword. Thanks again :)

Ben :)
September 09, 2012
On 09/09/2012 14:28, Timon Gehr wrote:
> I think so. Probably it is just a bug. (the spec does not indicate that
> it is illegal iirc.) Maybe you can even fix it yourself, the parser
> should be quite hackable.

That's good to hear - but since I'm only using D for a project outside of work, and have very little time for it as it is, I don't think I can offer to fix it. I'll probably just use the brackets for now. :)
September 09, 2012
On 09/09/2012 03:40 PM, Ben Davis wrote:
> On 09/09/2012 14:28, Timon Gehr wrote:
>> I think so. Probably it is just a bug. (the spec does not indicate that
>> it is illegal iirc.) Maybe you can even fix it yourself, the parser
>> should be quite hackable.
>
> That's good to hear - but since I'm only using D for a project outside
> of work, and have very little time for it as it is, I don't think I can
> offer to fix it. I'll probably just use the brackets for now. :)

OK, I'll give it a try and maybe submit a pull request.
September 09, 2012
On 09/09/2012 03:28 PM, Timon Gehr wrote:
> ...
>
> I think so. Probably it is just a bug. (the spec does not indicate that
> it is illegal iirc.)

I was wrong, the grammar actually precludes this, so it has to be
changed as well. Anyway, this is the pull request:
https://github.com/D-Programming-Language/dmd/pull/1111


September 09, 2012
On 09/09/2012 14:39, Ben Davis wrote:
>
> On 09/09/2012 14:23, Philippe Sigaud wrote:
>  > you could use factory functions:
>  >
>  > class A {}
>  >
>  > A a() { return new A();}
>
> Good idea. It might not work so well for me because the user might also
> be creating custom GUI components and would therefore have to create
> their own factory functions, but thanks for the idea in any case.

Maybe you could use a template:

auto create(T, Args...)(Args args)
{
    return new T(args);
}

class A
{
    int i;
    this(int i){this.i = i;}
}

import std.stdio;

void main()
{
    create!A(5).i.writeln();
}

But I agree with you it would be nice if dmd supported 'new A().b()'.
September 09, 2012
On 2012-09-09 17:16, Nick Treleaven wrote:

> But I agree with you it would be nice if dmd supported 'new A().b()'.

This would be good for DWT as well, since this is legal in Java.

-- 
/Jacob Carlborg
September 09, 2012
On Sun, Sep 9, 2012 at 5:16 PM, Nick Treleaven <ntrel-public@yahoo.co.uk> wrote:

> Maybe you could use a template:
>
> auto create(T, Args...)(Args args)
> {
>     return new T(args);
> }
>
> class A
> {
>     int i;
>     this(int i){this.i = i;}
> }
>
> import std.stdio;
>
> void main()
> {
>     create!A(5).i.writeln();
> }

A generic 'create' template would be good for generic code: on a reference type, use 'new' and pass args, whereas for a value type, use the type directly. Of course, this should manage builtins (like int, int[], int[3]) correctly.
« First   ‹ Prev
1 2