www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does D have syntax for adding subscopes to classes?

reply "GregoryP" <doo87bdad ffsds.com> writes:
I'm just wondering if, or how much of the following is possible 
in some way in D:

class Foo {
     int x;
     sub Bar {
         int x;
         int getFooX(){ return super.x; }
         sub FooBar {
             int x;
             int y;
             int addXes(){ return x + super.x + super.super.x; }
         }
     }
}

Where the Xes are accessible outside the class by Foo.x, 
Foo.Bar.x, Foo.Bar.FooBar.x.
Aug 12 2015
parent reply "sigod" <sigod.mail gmail.com> writes:
On Wednesday, 12 August 2015 at 15:21:28 UTC, GregoryP wrote:
 I'm just wondering if, or how much of the following is possible 
 in some way in D:

 class Foo {
     int x;
     sub Bar {
         int x;
         int getFooX(){ return super.x; }
         sub FooBar {
             int x;
             int y;
             int addXes(){ return x + super.x + super.super.x; }
         }
     }
 }

 Where the Xes are accessible outside the class by Foo.x, 
 Foo.Bar.x, Foo.Bar.FooBar.x.
[Nested classes][0] maybe? [0]: http://dlang.org/class.html#nested
Aug 12 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 12 August 2015 at 15:24:43 UTC, sigod wrote:
 [Nested classes][0] maybe?
Example with them: class Foo { int x; class Bar_ { // underscore cuz we have to declare variable too int x; int getFooX() { return this.outer.x; } class FooBar_ { int x; int y; int addXes() { return x + this.outer.x + this.outer.outer.x; } } FooBar_ FooBar; this() { FooBar = new FooBar_; // the var must also be initialized in a ctor } } Bar_ Bar; this() { Bar = new Bar_; // same out here } } void main() { auto foo = new Foo(); import std.stdio; writeln(foo.Bar.FooBar.addXes()); }
Aug 12 2015
parent "Xinok" <xinok live.com> writes:
On Wednesday, 12 August 2015 at 15:47:37 UTC, Adam D. Ruppe wrote:
 Example with them:
 ...
That's the idea I had except I would use a struct instead because using a class requires a second allocation.
Aug 12 2015