Improvement to struct inheritance possible?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Jan 18 21:06:22 UTC 2024


On Thursday, January 18, 2024 12:10:49 AM MST Danilo via Digitalmars-d wrote:
> The following code does not work, and I think it may be a bug or
> could need an enhancement:
...

> Could this please get improved a little bit, so "inherited"
> structure members using `alias this` are already available for
> constructor paramaters?


The purpose of alias this is to add an implicit conversion to the target.
Inside of a struct, it does do some extra work when a symbol isn't
recognized and then implicitly converts the this parameter using the alias
this to see if that allows the symbol to work, but at its core, alias this
really has nothing to do with inheritance. It's just an implicit conversion
(and if anything, personally, I think that doing the implicit conversion on
the this reference when it's not even explicitly used is just begging for
trouble, and I wish that that had not been implemented).

As for typeof, having it do implicit conversions seems like it would just be
an avenue for bugs. And the situation with classes is completely different,
because the members of a base class are actually members of the derived
class, not simply brought in via implicit conversion.

In general, alias this is a giant footgun - especially in the face of any
kind of type introspection - and IMHO, you really should reconsider what
you're doing any time that you use it. Unfortunately, it can be necessary
for certain classes of things, but extending it to do more is probably the
exact opposite of what we should be doing with it if we change it.

-  Jonathan M Davis





More information about the Digitalmars-d mailing list