Can we just have struct inheritence already?
bioinfornatics
bioinfornatics at fedoraproject.org
Fri Jun 21 22:19:12 UTC 2019
On Sunday, 9 June 2019 at 08:05:34 UTC, Manu wrote:
> I am really really tired of this pattern:
>
> struct DerivedStruct
> {
> static if (BaseStruct.tupleof.length > 0)
> BaseStruct base;
> else
> ref inout(BaseStruct) base() inout { return
> *cast(inout(BaseStruct)*)&this; }
> alias base this;
>
> // derived members
> //...
> }
>
> Imagine if we could just write:
>
> struct DerivedStruct : BaseStruct
> {
> // derived members
> //...
> }
>
> Just imagine!
That is a good idea (ahem… )but what about multiple derived
classes ?
I think the composition approach could help us by the use of the
delegator concept from Ruby.
With a such approach a struct can:
- own multiple base/child struct/component
- choose which method to forward to the base/child
struct/component
So at the and a such feature could be a sugar syntax of template
Proxy: https://dlang.org/library/std/typecons/proxy.html
Reference:
- ruby delegator:
https://blog.lelonek.me/how-to-delegate-methods-in-ruby-a7a71b077d99
- ruby delagator all supported methods:
https://rubyreferences.github.io/rubyref/stdlib/patterns/delegate.html
As Example
struct Person {
…
int get_age(){…}
}
struct Career{
…
int get_experience(){…}
}
struct Employee{
delegate: get_age, to: _person
delegate: get_experience, to: _career
immutable Person _person;
immutable Career _career;
this( immutable Person person , immutable Career career ){
_person = person;
_career = career;
}
}
More information about the Digitalmars-d
mailing list