Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
October 04, 2013 How to get runtime type of this? | ||||
---|---|---|---|---|
| ||||
import std.stdio; class A { static int id = 0; this() { writeln("typeid=", typeid(this)); writeln("id=",typeof(this).id); //how to get runtime type of this ?? } } class B : A { static int id = 1; } class C : A { static int id = 2; } void main() { A a = new B; A b = new C; } typeof(this) can get compile time type while typeid yield a runtime TypeInfo instance, but how to get runtime type? I want the output to be "id=1" and "id=2" respectively. |
October 04, 2013 Re: How to get runtime type of this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhouxuan | On 2013-10-04 10:03, Zhouxuan wrote: > import std.stdio; > > class A > { > static int id = 0; > this() > { > writeln("typeid=", typeid(this)); > writeln("id=",typeof(this).id); //how to get runtime type of > this ?? > } > } > > class B : A > { > static int id = 1; > } > > class C : A > { > static int id = 2; > } > > void main() > { > A a = new B; > A b = new C; > } > > typeof(this) can get compile time type while typeid yield a runtime > TypeInfo instance, but how to get runtime type? I want the output to be > "id=1" and "id=2" respectively. You can do this: class A { static int id = 0; this(this T)() { writeln("typeid=", typeid(T)); writeln("id=",T.id); //how to get runtime type of this ?? } } class B : A { static int id = 1; this () { super(); } } class C : A { static int id = 2; this () { super(); } } void main() { A a = new B; A b = new C; } Put I'm guess you want to avoid the constructor in the subclasses. I think there's a bug report about this. -- /Jacob Carlborg |
October 04, 2013 Re: How to get runtime type of this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Friday, 4 October 2013 at 08:23:11 UTC, Jacob Carlborg wrote:
> On 2013-10-04 10:03, Zhouxuan wrote:
>> import std.stdio;
>>
>> class A
>> {
>> static int id = 0;
>> this()
>> {
>> writeln("typeid=", typeid(this));
>> writeln("id=",typeof(this).id); //how to get runtime type of
>> this ??
>> }
>> }
>>
>> class B : A
>> {
>> static int id = 1;
>> }
>>
>> class C : A
>> {
>> static int id = 2;
>> }
>>
>> void main()
>> {
>> A a = new B;
>> A b = new C;
>> }
>>
>> typeof(this) can get compile time type while typeid yield a runtime
>> TypeInfo instance, but how to get runtime type? I want the output to be
>> "id=1" and "id=2" respectively.
>
> You can do this:
>
> class A
> {
> static int id = 0;
> this(this T)()
> {
> writeln("typeid=", typeid(T));
> writeln("id=",T.id); //how to get runtime type of this ??
> }
> }
>
> class B : A
> {
> static int id = 1;
> this () { super(); }
> }
>
> class C : A
> {
> static int id = 2;
> this () { super(); }
> }
>
> void main()
> {
> A a = new B;
> A b = new C;
> }
>
> Put I'm guess you want to avoid the constructor in the subclasses. I think there's a bug report about this.
This is exactly what I want, thank you very much!
Redundant constructor here is not a problem, however there're so many workarounds atm.
|
October 04, 2013 Re: How to get runtime type of this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Friday, 4 October 2013 at 08:23:11 UTC, Jacob Carlborg wrote:
> On 2013-10-04 10:03, Zhouxuan wrote:
>> import std.stdio;
>>
>> class A
>> {
>> static int id = 0;
>> this()
>> {
>> writeln("typeid=", typeid(this));
>> writeln("id=",typeof(this).id); //how to get runtime type of
>> this ??
>> }
>> }
>>
>> class B : A
>> {
>> static int id = 1;
>> }
>>
>> class C : A
>> {
>> static int id = 2;
>> }
>>
>> void main()
>> {
>> A a = new B;
>> A b = new C;
>> }
>>
>> typeof(this) can get compile time type while typeid yield a runtime
>> TypeInfo instance, but how to get runtime type? I want the output to be
>> "id=1" and "id=2" respectively.
>
> You can do this:
>
> class A
> {
> static int id = 0;
> this(this T)()
> {
> writeln("typeid=", typeid(T));
> writeln("id=",T.id); //how to get runtime type of this ??
> }
> }
>
> class B : A
> {
> static int id = 1;
> this () { super(); }
> }
>
> class C : A
> {
> static int id = 2;
> this () { super(); }
> }
>
> void main()
> {
> A a = new B;
> A b = new C;
> }
>
> Put I'm guess you want to avoid the constructor in the subclasses. I think there's a bug report about this.
Unfortunately it doesn't work if C inherits from B.
|
October 04, 2013 Re: How to get runtime type of this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhouxuan | I am afraid if you want true polymorphic behavior, `id` needs to become a virtual getter function. D runtime reflection is quite lacking in that are. |
October 04, 2013 Re: How to get runtime type of this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhouxuan | On 2013-10-04 10:42, Zhouxuan wrote: > Unfortunately it doesn't work if C inherits from B. What you need is a template constructor in B, just as in A. But it seems it's not possible to forward the template type to the base class. -- /Jacob Carlborg |
Copyright © 1999-2021 by the D Language Foundation