How to get to a class initializer through introspection?

Adam D. Ruppe destructionator at
Mon Aug 3 01:06:52 UTC 2020

On Monday, 3 August 2020 at 00:37:48 UTC, Andrei Alexandrescu 
> Holy Molly this works. FWIW here's what I plan to use: 
> No need for __gshared because 
> immutable stuff is already shared.

Yes, indeed.

> The only bummer is it can't be read during compilation, but I 
> assume there'd be a chicken and egg problem if it were.

Eh, what if you did

class A {
     ubyte[__traits(classInstanceSize, A)] recursive;

it gives a forward reference error, but it works elsewhere and I 
think that's fair. We could probably do something similar with a 
hypothetical __traits(initializer).

So I think it is potentially possible and worth at least looking 
more into. My pragma(mangle) thing there was just a hack to make 
it work with today's dmd, but tomorrow's dmd could do a better 

(what set me on this path btw was just a hunch I could put opCast 
in Object itself and hack up my own dynamic cast without 
druntime. Obviously, I didn't finish that for the blog, but it 
*is* possible, at least if you explicitly mixin something. That 
is what led me to posting this thread to muse how to change that: but it is still awkward to deal with interface offsets without help from the compiler regardless. But still prolly doable with some more time.)

> Thanks, Adam! Walter and I had absolutely no idea this can be 
> done but we thought to post here on the off chance. Thanks 
> again!

Y'all should read my blog :P

There's times when I go a full month with nothing to say since I 
get busy with day job work or around the house with family stuff 
etc. But I still try to slap something down at least once a month 
and I have a lot of magic tricks and just cool library stuff in 

Next week I'll probably post my little Tetris game I slapped 
together on an airplane a couple weeks ago. So not much to learn 
about D magic tricks but sometimes it is nice to just show how 
easy fun things are to do too!

More information about the Digitalmars-d mailing list