Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 22, 2014 Simple code sample of Nesting Structures. I'm I doing something illegal here? | ||||
---|---|---|---|---|
| ||||
// 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 = |
November 22, 2014 Re: Simple code sample of Nesting Structures. I'm I doing something illegal here? | ||||
---|---|---|---|---|
| ||||
Posted in reply to WhatMeWorry | 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.
|
November 22, 2014 Re: Simple code sample of Nesting Structures. I'm I doing something illegal here? | ||||
---|---|---|---|---|
| ||||
Posted in reply to WhatMeWorry Attachments: | On Sat, 22 Nov 2014 20:57:05 +0000 WhatMeWorry via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > 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. |
November 22, 2014 Re: Simple code sample of Nesting Structures. I'm I doing something illegal here? | ||||
---|---|---|---|---|
| ||||
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: > > > 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. 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? |
November 22, 2014 Re: Simple code sample of Nesting Structures. I'm I doing something illegal here? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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
|
Copyright © 1999-2021 by the D Language Foundation