Dynamic templated virtuals - I still often want them
Arafel
er.krali at gmail.com
Thu Jul 23 15:39:08 UTC 2020
On 23/7/20 17:00, Max Samukha wrote:
> WIW, a more general way to initialize an object with static type data
> would be (currently it fails for indirectly derived classes due to a
> compiler bug):
>
> class Base {
> this(this This)() {
> _serialize = function(Base base) {
> // base can be safely reinterpreted into This here if needed
>
> return This.stringof; // just for example
> };
> }
>
> final string serialize() {
> return _serialize(this);
> }
>
> private string function(Base) _serialize;
> }
>
> class Derived: Base {
> }
>
> void main() {
> Base b = new Derived;
> assert(b.serialize() == "Derived");
> }
>
Good that I read all the replies before hitting "send", because I was
going to propose exactly this :-)
My only doubt is how it would interact with constructors defined in the
derived classes. I think I tried doing something like this some time
ago, but I couldn't make it work in the end because the `super`
constructor wasn't being called.
Being able to register classes when there are first instantiated would
be a useful workaround.
> Also, there seems to be no good reason "this T" should not work for any
> class member, including class static constructors.
This seems to be regularly reported as a bug, I posted earlier some of
them. I would really love to see this work, and not just for static
functions, also for enums, aliases or actually any other kind of template.
More information about the Digitalmars-d
mailing list