Struct inheritance
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Fri Dec 6 01:13:04 UTC 2024
On 06/12/2024 2:04 PM, Jonathan M Davis wrote:
> On Thursday, December 5, 2024 5:35:38 PM MST Richard (Rikki) Andrew Cattermole
> via dip.ideas wrote:
>> For the checking of inheritance what I mean is:
>>
>> ``is(Child : Parent)``
>
> Except that that _doesn't_ check for inheritance. It checks for an implicit
> conversion. It's just used for checking for inheritance based on the fact
> that a reference to a child class can implicitly convert to a reference of
> its parent class. It's already fundamentally broken in the sense that alias
> this can make it incorrect if you're testing for inheritance, e.g.
>
> ```
> void main()
> {
> auto foo = new Foo;
> A a = foo;
>
> static assert(is(Foo : A));
> }
>
> class A {}
>
> class Foo
> {
> A a;
>
> alias this = a;
> }
> ```
>
> Really, we need something in __traits to test for inheritance and to stop
> using is expressions for it.
>
> Either way, it's fundamentally wrong to have is(Child : Parent) tell you
> that a struct named Child inherits from a struct named Parent, because they
> wouldn't have an implicit conversion, and : is testing specifically for an
> implicit conversion.
>
> - Jonathan M Davis
Problem is, template parameters also should work, using ``Child :
Parent`` syntax.
Otherwise the usability on this is going to be absolutely atrocious in
meta-programming.
More information about the dip.ideas
mailing list