digitalmars.D - It is a bug ?
- Du Liang <duliang.21 163.com> May 06 2009
- Jarrett Billingsley <jarrett.billingsley gmail.com> May 06 2009
- Georg Wrede <georg.wrede iki.fi> May 06 2009
- Georg Wrede <georg.wrede iki.fi> May 06 2009
- grauzone <none example.net> May 06 2009
- Du Liang <duliang.21 163.com> May 06 2009
- grauzone <none example.net> May 07 2009
- "Denis Koroskin" <2korden gmail.com> 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:=A0 =A0 =A0 =A0int[] arrB =3D [2,2,2]; // arrB is static in[] =A0or 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 =3D arrB.dup;" in this().
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 ?
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 ?
of AB. You should initialize it in this() instead, or put "arrB = arrB.dup;" in this().
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 ?
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 06 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 ?
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 06 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 ?
of AB. You should initialize it in this() instead, or put "arrB = arrB.dup;" in this().
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.
May 07 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









Georg Wrede <georg.wrede iki.fi> 