View mode: basic / threaded / horizontal-split · Log in · Help
September 10, 2012
Re: new A().b() currently requires extra brackets/parentheses
Am 09.09.2012 16:43, schrieb Timon Gehr:
> 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
> 
> 

Does the following also work?

class A {
	static class B {
		int x;
	}
}

new A.B.x = 1;
September 10, 2012
Re: new A().b() currently requires extra brackets/parentheses
On 09/10/2012 09:01 AM, Sönke Ludwig wrote:
> Am 09.09.2012 16:43, schrieb Timon Gehr:
>> 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
>>
>>
>
> Does the following also work?
>
> class A {
> 	static class B {
> 		int x;
> 	}
> }
>
> new A.B.x = 1;
>

A.B.x is not a type.

new A.B().x = 1 works.
September 10, 2012
Re: new A().b() currently requires extra brackets/parentheses
On 09/09/2012 18:25, Philippe Sigaud wrote:
> 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.

I assumed he only needed class construction. I forgot that we already 
have std.container.make (which also works with structs):

make!A(5).i.writeln();

I noticed jmdavis has a pull request for what you describe:
https://github.com/D-Programming-Language/phobos/pull/756
September 10, 2012
Re: new A().b() currently requires extra brackets/parentheses
On Mon, Sep 10, 2012 at 7:38 PM, Nick Treleaven
<ntrel-public@yahoo.co.uk> wrote:

> I assumed he only needed class construction. I forgot that we already have
> std.container.make (which also works with structs):

Oh, I wasn't criticizing your code, just listing my needs :)
I didn't know about std.container.make.

> make!A(5).i.writeln();
>
> I noticed jmdavis has a pull request for what you describe:
> https://github.com/D-Programming-Language/phobos/pull/756

Good!
September 10, 2012
Re: new A().b() currently requires extra brackets/parentheses
Am 10.09.2012 15:39, schrieb Timon Gehr:
> On 09/10/2012 09:01 AM, Sönke Ludwig wrote:
>> Am 09.09.2012 16:43, schrieb Timon Gehr:
>>> 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
>>>
>>>
>>
>> Does the following also work?
>>
>> class A {
>>     static class B {
>>         int x;
>>     }
>> }
>>
>> new A.B.x = 1;
>>
> 
> A.B.x is not a type.
> 
> new A.B().x = 1 works.

Of course it's not a type... but A.B is. It's also not that important,
but it feels slightly inconsistent and coincidential that the clamps are
used here to separate the new expression from the rest.
September 10, 2012
Re: new A().b() currently requires extra brackets/parentheses
On 9/10/12, Sönke Ludwig <sludwig@outerproduct.org> wrote:
> Of course it's not a type... but A.B is. It's also not that important,
> but it feels slightly inconsistent and coincidential that the clamps are
> used here to separate the new expression from the rest.

It prevents ambiguity and hijacking.

If 'x' of B ends up being a type or an alias of a type you'll end up
instantiating something else instead of the B class.
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home