Thread overview |
---|
August 18, 2016 Virtual Classes? | ||||
---|---|---|---|---|
| ||||
https://en.wikipedia.org/wiki/Virtual_class Can D do stuff like this naturally? |
August 18, 2016 Re: Virtual Classes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Engine Machine | On Thursday, 18 August 2016 at 00:49:49 UTC, Engine Machine wrote: > https://en.wikipedia.org/wiki/Virtual_class > > Can D do stuff like this naturally? Not naturally. The ancestor must be specified for the inner "virtual class": °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°° class Foo { class Internal { void stuff() {} } } class Bar: Foo { class Internal: Foo.Internal { override void stuff() {} } } °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°° However the ancestor inner class is well inherited (in the scope of a derived class Internal resolves to the parent definition or to the internal override when applicable). Note that I find the wikipedia example very bad. "Parts" doesn't define anything to override. |
August 18, 2016 Re: Virtual Classes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Engine Machine | On Thursday, 18 August 2016 at 00:49:49 UTC, Engine Machine wrote: > https://en.wikipedia.org/wiki/Virtual_class > > Can D do stuff like this naturally? Yes, D's `alias this` feature supports this. https://dlang.org/spec/class.html#alias-this |
August 18, 2016 Re: Virtual Classes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Thursday, 18 August 2016 at 02:51:48 UTC, Meta wrote:
> On Thursday, 18 August 2016 at 00:49:49 UTC, Engine Machine wrote:
>> https://en.wikipedia.org/wiki/Virtual_class
>>
>> Can D do stuff like this naturally?
>
> Yes, D's `alias this` feature supports this.
>
> https://dlang.org/spec/class.html#alias-this
No read carefully, alias this does not the same thing, particularly when the time comes to override the inner type.
|
August 18, 2016 Re: Virtual Classes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Thursday, 18 August 2016 at 02:55:49 UTC, Basile B. wrote:
> On Thursday, 18 August 2016 at 02:51:48 UTC, Meta wrote:
>> On Thursday, 18 August 2016 at 00:49:49 UTC, Engine Machine wrote:
>>> https://en.wikipedia.org/wiki/Virtual_class
>>>
>>> Can D do stuff like this naturally?
>>
>> Yes, D's `alias this` feature supports this.
>>
>> https://dlang.org/spec/class.html#alias-this
>
> No read carefully, alias this does not the same thing, particularly when the time comes to override the inner type.
How doesn't it? You define a member with the same name in the outer class and it'll override the inner one.
|
August 18, 2016 Re: Virtual Classes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Thursday, 18 August 2016 at 03:58:00 UTC, Meta wrote: > On Thursday, 18 August 2016 at 02:55:49 UTC, Basile B. wrote: >> On Thursday, 18 August 2016 at 02:51:48 UTC, Meta wrote: >>> On Thursday, 18 August 2016 at 00:49:49 UTC, Engine Machine wrote: >>>> https://en.wikipedia.org/wiki/Virtual_class >>>> >>>> Can D do stuff like this naturally? >>> >>> Yes, D's `alias this` feature supports this. >>> >>> https://dlang.org/spec/class.html#alias-this >> >> No read carefully, alias this does not the same thing, particularly when the time comes to override the inner type. > > How doesn't it? You define a member with the same name in the outer class and it'll override the inner one. You can't call the most derived from a variable that has a lesser derived type: °°°°°°°°°°°°°°°°°°°°°°°° class Foo { Internal internal; class Internal {void stuff() {"base".writeln;}} this() {internal = new Internal;} alias internal this; } class Bar: Foo { void stuff() {"derived".writeln;} } void main(string[] args) { Foo f = new Bar; f.stuff(); // "base", not "derived". } °°°°°°°°°°°°°°°°°°°°°°°°° From what i've read, "virtual classes" respect the OOP principles. |
Copyright © 1999-2021 by the D Language Foundation