Struct inheritance

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Wed Dec 4 12:43:49 UTC 2024


On 05/12/2024 1:03 AM, Derek Fawcus wrote:
> On Wednesday, 4 December 2024 at 11:51:57 UTC, Richard (Rikki) Andrew 
> Cattermole wrote:
>> On 05/12/2024 12:38 AM, Derek Fawcus wrote:
>>> For which I get:
>>>
>>> ```
>>> MethB(P) Direct Parent
>>> MethC(P) Direct Parent
>>>
>>> MethB(P) Inherit Parent
>>
>> Due to no vtables, you cannot override a parent method and have it see 
>> the override.
>>
>> MethC(C) Inherit Parent
>>
>>> ```
>>
>> The last example, assignment, this requires vtables and casting up.
>>
>> Which structs cannot do.
> 
> So are you saying all three would be like the 'Direct Parent' case?
> 
> Presumably by making the Assign case cheat (manually forcibly casting a 
> pointer of the child to a pointer of the parent), it would also yield 
> the same result.

Casting a pointer to the parent, would be ``@system``, so what it does 
is very much "good luck with that".

> i.e.:
> 
> ```
> MethB(P) Direct Parent
> MethC(P) Direct Parent
> 
> MethB(P) Inherit Parent
> MethC(P) Inherit Parent
> 
> MethB(P) Assign Parent
> MethC(P) Assign Parent
> ```
> 
> Or for the child method override, would you simply yield a compile 
> failure - as it can never work, and has no backward compatibility issue?

In a situation such as:

```d
struct Parent {
	void method1() {
		method2();
	}

	void method2() {
	}
}

struct Child : Parent {
	override void method2() {
	}
}
```

My general view is that you have to be a little bit smart about it.

There may be reasons you would want something like this, but there is 
also reasons it might not be desired.

I want a way to reinterpret parent methods as if it was in the child if 
not overriden. At which point ``method1`` would see the child 
``method2`` rather than the one in the parent.



More information about the dip.ideas mailing list