Thread overview | ||||||
---|---|---|---|---|---|---|
|
August 12, 2015 Does D have syntax for adding subscopes to classes? | ||||
---|---|---|---|---|
| ||||
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. |
August 12, 2015 Re: Does D have syntax for adding subscopes to classes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to GregoryP | 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 |
August 12, 2015 Re: Does D have syntax for adding subscopes to classes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to sigod | 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()); } |
August 12, 2015 Re: Does D have syntax for adding subscopes to classes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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.
|
Copyright © 1999-2021 by the D Language Foundation