March 24, 2017 Virtual nested classes and "this" | ||||
|---|---|---|---|---|
| ||||
Hi,
I have been poking around with overriding internal classes, and after reading [1] it was actually not clear to me whether it could be done or not, so I started trying.
The good news (for me, at least) is that it can mostly be done [2], whoever I have found a bit intriguing that I need to explicitly use "this.i" instead of just "i" in B.fb() [3].
Just in case, the code I got to work is this:
```
class A {
public static class I {
public string fai() {
return "A.I.fai";
}
}
public string fa() {
return i.fai();
}
public this(this C)() {
i_ = new C.I();
}
protected I i_;
public @property T.I i(this T)() { return cast(T.I) this.i_; }
}
class B : A {
override public static class I : A.I {
override public string fai() {
return "B.I.fai";
}
public string fbi() {
return "B.I.fbi";
}
}
public this(this C)() {
super();
}
public string fb() {
return this.i.fbi(); // Why is "this" needed here?
}
}
void main() {
A a = new A();
A ab = new B();
B b = new B();
assert (a.fa() == "A.I.fai");
assert (ab.fa() == "B.I.fai");
assert (b.fa() == "B.I.fai");
assert (b.fb() == "B.I.fbi");
}
```
Is there a reason for that? Why cannot it be inferred as in the regular case?
Also, if there's a way to do it without using the property wrapper, I'd be glad to know it :)
I tried something like:
```
template i(this T) {
T.I i;
}
```
but it didn't like it... I guess members have to be better defined...
Best,
A
[1]: https://forum.dlang.org/thread/siwjqxiuocqtrldczand@forum.dlang.org
[2]: https://dpaste.dzfl.pl/8f4e0df438e5
[3]: https://dpaste.dzfl.pl/8f4e0df438e5#line-34
| ||||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply