On 2/8/20 7:39 PM, realhet wrote:
On Sunday, 9 February 2020 at 00:15:47 UTC, Drug wrote:
On Saturday, 8 February 2020 at 23:37:56 UTC, realhet wrote:
Hello,
class A{
int i = 42;
}
Is there a way to get that 42? (Input is the class A and the name of
the field 'i')
A.i.init returns 0, so it doesn't care about the class.
classinfo.m_init can be an option, but maybe there's a nicer way to
do this?
Thx!
Try A.init.i
I tried and A.init.stringof == "void()"
That generates a new question, what is void() ?
It has no sizeof property.
I guess it's the voidest of the voids in D :D
Looks like you wrote a function void init() inside your class.
Don't do that. D has a default init property.
But as an aside, A.init has a value of null. So A.init.i => segfault.
The 42 lives inside the intializer, which is a void array stored in the
TypeInfo. But you probably don't want to get it that way.
Adam's way is technically a good way to get it. But you don't
necessarily need that (and it might not be what you are looking for --
the constructor could easily change i to a different value). Problem is
that typeid cannot be read at compile time.
https://issues.dlang.org/show_bug.cgi?id=7147
If you could do that, you could construct a "pre-constructed" class
instance, and use it as the prototype for reading the int value.
-Steve