Struct inheritance

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Dec 6 01:04:37 UTC 2024


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





More information about the dip.ideas mailing list