digitalmars.D.learn - Simple code sample of Nesting Structures. I'm I doing something
- WhatMeWorry (56/56) Nov 22 2014 // Two simple value type structures. one embedded in the other.
- Chris Nicholson-Sauls (2/3) Nov 22 2014 That 'auto' is the problem. You want 'this.bottom = ...' instead.
- ketmar via Digitalmars-d-learn (8/9) Nov 22 2014 On Sat, 22 Nov 2014 20:57:05 +0000
- bearophile (4/7) Nov 22 2014 I told ya.
- H. S. Teoh via Digitalmars-d-learn (8/19) Nov 22 2014 Yeah, this bit me before, and it was Not Fun. Generally, it's a very bad
// Two simple value type structures. one embedded in the other. I've stepped through the debugger and I see the embedded structure being set to 2, and dog. import std.stdio; struct NestedBottom { int i; char[3] fixedArray; // this(){} no-argument ctor can only be defined by the compiler this(int i, char[3] fixedArray) { this.i = i; this.fixedArray = fixedArray; } void print() { writeln("This is level ", i); writeln("animal = ", fixedArray); } } struct NestedTop { int i; char[3] fixedArray; NestedBottom bottom; // this(){} no-argument ctor can only be defined by the compiler this(int i, char[3] fixedArray) { this.i = i; this.fixedArray = fixedArray; auto bottom = NestedBottom(2, ['d','o','g']); } void print() { writeln("This is level ", i); writeln("animal = ", fixedArray); bottom.print(); // added this in desperation. Still nothing. writeln("This is level ", bottom.i); writeln("animal = ", bottom.fixedArray); } } void main() { auto top = NestedTop(1, ['c', 'a', 't']); top.print(); } Output is the following: This is level 1 animal = cat This is level 0 animal = This is level 0 animal =
Nov 22 2014
On Saturday, 22 November 2014 at 20:57:07 UTC, WhatMeWorry wrote:auto bottom = NestedBottom(2, ['d','o','g']);That 'auto' is the problem. You want 'this.bottom = ...' instead.
Nov 22 2014
On Sat, 22 Nov 2014 20:57:05 +0000 WhatMeWorry via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:auto bottom =3D NestedBottom(2, ['d','o','g']);ah, that good old thingy! there were some debates about locals that shadows fields and how that can introduce some hard-to-catch bugs. you were hit by exactly that: you creating local variable `bottom` instead of assigning value to member `bottom`. that nasty little compiler is playing tricks with us all.
Nov 22 2014
ketmar:there were some debates about locals that shadows fields and how that can introduce some hard-to-catch bugs.I told ya. Bye, bearophile
Nov 22 2014
On Sat, Nov 22, 2014 at 11:54:01PM +0200, ketmar via Digitalmars-d-learn wrote:On Sat, 22 Nov 2014 20:57:05 +0000 WhatMeWorry via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Yeah, this bit me before, and it was Not Fun. Generally, it's a very bad idea to name local variables after field members due to shadowing. I'd file a bug for it, but I'm not sure how likely it is to get fixed. But you could try. ;-) (Sometimes it works. :-P) T -- Leather is waterproof. Ever see a cow with an umbrella?auto bottom = NestedBottom(2, ['d','o','g']);ah, that good old thingy! there were some debates about locals that shadows fields and how that can introduce some hard-to-catch bugs. you were hit by exactly that: you creating local variable `bottom` instead of assigning value to member `bottom`. that nasty little compiler is playing tricks with us all.
Nov 22 2014