Can we just have struct inheritence already?

bioinfornatics bioinfornatics at fedoraproject.org
Sat Jun 22 06:59:08 UTC 2019


On Saturday, 22 June 2019 at 06:29:43 UTC, Ola Fosheim Grøstad 
wrote:
> On Friday, 21 June 2019 at 22:19:12 UTC, bioinfornatics wrote:
>> struct Employee{
>>   delegate: get_age, to: _person
>>   delegate: get_experience, to: _career
>>
>>   immutable Person _person;
>>   immutable Career _career;
>
> Listing all methods isn't very helpful, if that is the 
> suggestion. Then a more consistent syntax would be
>
> struct Employee {
> alias getage = _person.getage;
> alias get_experience = _career.get_experience;
>
>
> Yet, that does not solve the MI problems.  Some are:
>
> 1. What to do if you both superclasses has a length() method.
>
> 2. What do do if both superclasses has used the same method 
> name for unrelated concepts?
>
> If there is no overlap semantically then problem 1 and 2 can be 
> resolved with a cast to the super class, so that you get the 
> right method.
>
> However, what if the subclass has to modify them to retain the 
> correct semantics? Which is really what MI problems are about.
>
> Then you need som way of overloading a function that calls a 
> specific attribute of  a struct. So you need overloading on 
> attributes, not only overloading on types, yet you also need a 
> reference to the parent struct.
>
> So you need to be able to do something like
>
> int length(SubStruct.attributename* this) {
>     this.parent
> }
>
>
> Ola.
Yes you are right, previously I show one sample case, when you 
want to choose which method will be forwarded to the component.


If you need to forward all child methods from the base struct you 
can use the template proxy from dlang or alias this.

For MI case, as here is a composition:
(i)a compiler's warning could be raised if method shadow another 
one
(ii) a sugar syntax to "alias" to synonym method name

delegate: @Person.size, to: size
delegate: @Career.size, to: length

(iii) a sugar syntax to forward all
delegate: @Person

Is an example of syntax using @annotation is possible to




More information about the Digitalmars-d mailing list