Thread overview
It is a bug ?
May 06, 2009
Du Liang
May 06, 2009
Denis Koroskin
May 06, 2009
Georg Wrede
May 06, 2009
Georg Wrede
May 06, 2009
grauzone
May 07, 2009
Du Liang
May 07, 2009
grauzone
May 06, 2009
import std.stdio;
class AB{
	int A;
	int B = 2;
	int[] arrA;
	int[] arrB = [2,2,2]; // arrB is static in[]  or bug ?
	this(){
		this.A = 1;
		this.arrA=[1,1,1];
	}
}
void main(){
	AB ab1 = new AB();
	AB ab2 = new AB();
	writeln(ab1.A, " | " ,ab1.B, " | " ,ab1.arrA, " | " ,ab1.arrB);
	writeln(ab2.A, " | " ,ab2.B, " | " ,ab2.arrA, " | " ,ab2.arrB);
	writeln("change...");
	ab1.A = 10;
	ab1.B = 20;
	ab1.arrA[0] = 10;
	ab1.arrB[0] = 20; // ab2.arrB = 20  why? bug?
	writeln(ab1.A, " | " ,ab1.B, " | " ,ab1.arrA, " | " ,ab1.arrB);
	writeln(ab2.A, " | " ,ab2.B, " | " ,ab2.arrA, " | " ,ab2.arrB);
	readln();
}
May 06, 2009
On Wed, May 6, 2009 at 10:06 AM, Du Liang <duliang.21@163.com> wrote:

>        int[] arrB = [2,2,2]; // arrB is static in[]  or bug ?

Declaring the variable like this uses a single array for all instances
of AB.  You should initialize it in this() instead, or put "arrB =
arrB.dup;" in this().
May 06, 2009
On Wed, 06 May 2009 19:33:03 +0400, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote:

> On Wed, May 6, 2009 at 10:06 AM, Du Liang <duliang.21@163.com> wrote:
>
>>        int[] arrB = [2,2,2]; // arrB is static in[]  or bug ?
>
> Declaring the variable like this uses a single array for all instances
> of AB.  You should initialize it in this() instead, or put "arrB =
> arrB.dup;" in this().

I believe this is a horrible inconsistency. It shouldn't be allowed in first place, because typeof([2,2,2]) is immutable(int)[] in this context.
May 06, 2009
Denis Koroskin wrote:
> On Wed, 06 May 2009 19:33:03 +0400, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote:
> 
>> On Wed, May 6, 2009 at 10:06 AM, Du Liang <duliang.21@163.com> wrote:
>>
>>>        int[] arrB = [2,2,2]; // arrB is static in[]  or bug ?
>> Declaring the variable like this uses a single array for all instances
>> of AB.  You should initialize it in this() instead, or put "arrB =
>> arrB.dup;" in this().
> 
> I believe this is a horrible inconsistency. It shouldn't be allowed in first place, because typeof([2,2,2]) is immutable(int)[] in this context.

Issue 2947 Submitted

May 06, 2009
Denis Koroskin wrote:
> On Wed, 06 May 2009 19:33:03 +0400, Jarrett Billingsley wrote:
>> On Wed, May 6, 2009 at 10:06 AM, Du Liang wrote:
>>
>>>        int[] arrB = [2,2,2]; // arrB is static in[]  or bug ?
>> Declaring the variable like this uses a single array for all instances
>> of AB.  You should initialize it in this() instead, or put "arrB =
>> arrB.dup;" in this().
> I believe this is a horrible inconsistency. It shouldn't be allowed
> in first place, because typeof([2,2,2]) is immutable(int)[] in this
> context.

writeln(typeof([2,2,2]).stringof);

int[3u]


A literal string seems not to be immutable.
May 06, 2009
Denis Koroskin wrote:
> On Wed, 06 May 2009 19:33:03 +0400, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote:
> 
>> On Wed, May 6, 2009 at 10:06 AM, Du Liang <duliang.21@163.com> wrote:
>>
>>>        int[] arrB = [2,2,2]; // arrB is static in[]  or bug ?
>> Declaring the variable like this uses a single array for all instances
>> of AB.  You should initialize it in this() instead, or put "arrB =
>> arrB.dup;" in this().
> 
> I believe this is a horrible inconsistency. It shouldn't be allowed in first place, because typeof([2,2,2]) is immutable(int)[] in this context.

DWIM would be to let the compiler automatically move these instructions into all ctors.

class A {
   int[] arrB = [2,2,2];
   X x = new X();
   this() {
       code1();
   }
   this(int) {
       code2();
   }
}

would be transformed into

class A {
   int[] arrB;
   X x;
   this() {
       arrB = [2,2,2];
       x = new X();
       code1();
   }
   this(int) {
       arrB = [2,2,2];
       x = new X();
       code2();
   }
}
May 07, 2009
grauzone Wrote:

> Denis Koroskin wrote:
> > On Wed, 06 May 2009 19:33:03 +0400, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote:
> > 
> >> On Wed, May 6, 2009 at 10:06 AM, Du Liang <duliang.21@163.com> wrote:
> >>
> >>>        int[] arrB = [2,2,2]; // arrB is static in[]  or bug ?
> >> Declaring the variable like this uses a single array for all instances
> >> of AB.  You should initialize it in this() instead, or put "arrB =
> >> arrB.dup;" in this().
> > 
> > I believe this is a horrible inconsistency. It shouldn't be allowed in first place, because typeof([2,2,2]) is immutable(int)[] in this context.
> 
> DWIM would be to let the compiler automatically move these instructions into all ctors.
> 
> class A {
>     int[] arrB = [2,2,2];
>     X x = new X();
>     this() {
>         code1();
>     }
>     this(int) {
>         code2();
>     }
> }
> 
> would be transformed into
> 
> class A {
>     int[] arrB;
>     X x;
>     this() {
>         arrB = [2,2,2];
>         x = new X();
>         code1();
>     }
>     this(int) {
>         arrB = [2,2,2];
>         x = new X();
>         code2();
>     }
> }

Thanks!
May 07, 2009
Du Liang wrote:
> grauzone Wrote:
> 
>> Denis Koroskin wrote:
>>> On Wed, 06 May 2009 19:33:03 +0400, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote:
>>>
>>>> On Wed, May 6, 2009 at 10:06 AM, Du Liang <duliang.21@163.com> wrote:
>>>>
>>>>>        int[] arrB = [2,2,2]; // arrB is static in[]  or bug ?
>>>> Declaring the variable like this uses a single array for all instances
>>>> of AB.  You should initialize it in this() instead, or put "arrB =
>>>> arrB.dup;" in this().
>>> I believe this is a horrible inconsistency. It shouldn't be allowed in first place, because typeof([2,2,2]) is immutable(int)[] in this context.
>> DWIM would be to let the compiler automatically move these instructions into all ctors.
>>
>> class A {
>>     int[] arrB = [2,2,2];
>>     X x = new X();
>>     this() {
>>         code1();
>>     }
>>     this(int) {
>>         code2();
>>     }
>> }
>>
>> would be transformed into
>>
>> class A {
>>     int[] arrB;
>>     X x;
>>     this() {
>>         arrB = [2,2,2];
>>         x = new X();
>>         code1();
>>     }
>>     this(int) {
>>         arrB = [2,2,2];
>>         x = new X();
>>         code2();
>>     }
>> }
> 
> Thanks!

Um... Maybe I should clarify: it does NOT work like that right now! My post was more like a feature request.