digitalmars.D - Can you use RTTI to determine a union member?
- Janice Caron (21/21) Oct 16 2007 Suppose I have
- Kirk McDonald (8/35) Oct 16 2007 enum + union is the age-old solution to this. D doesn't store this
- Daniel Keep (4/36) Oct 16 2007 Another solution is to use a variant datatype which will take care of
- BCS (7/33) Oct 16 2007 if A, B and C are classes then you can uses
- Christian Kamm (5/10) Oct 16 2007 If A, B, C are classes, you could also check with
- Janice Caron (5/9) Oct 16 2007 That's really cool, but I can't find any documentation on .classinfo.
- BCS (2/15) Oct 16 2007 look under the phobos docs
- Bill Baxter (4/23) Oct 16 2007 Specifically under "object", right at the top. There's a bunch of
- Frits van Bommel (2/7) Oct 16 2007 Only if the union never stores references to subclasses.
- 0ffh (7/15) Oct 16 2007 Righto, AFAIK; in that case one might consider to have all
Suppose I have union U { A a; B b; C c; } and a function f(U u) is there any way to determine at runtime, which union member has been assigned? I guess I want to do something like void f(U u) { if (is(u.a.typeof == A)) /* stuff */ else if (is(u.a.typeof == B)) /* stuff */ else if (is(u.a.typeof == C)) /* stuff */ } except obviously that won't work because typeof is a compile time thing, not a run time thing, and so it will always return A. I don't suppose there's a runtime solution? If not, I guess I'll just do what I've been doing all along (that is, store the choice in an enum outside the union).
Oct 16 2007
Janice Caron wrote:Suppose I have union U { A a; B b; C c; } and a function f(U u) is there any way to determine at runtime, which union member has been assigned? I guess I want to do something like void f(U u) { if (is(u.a.typeof == A)) /* stuff */ else if (is(u.a.typeof == B)) /* stuff */ else if (is(u.a.typeof == C)) /* stuff */ } except obviously that won't work because typeof is a compile time thing, not a run time thing, and so it will always return A. I don't suppose there's a runtime solution? If not, I guess I'll just do what I've been doing all along (that is, store the choice in an enum outside the union).enum + union is the age-old solution to this. D doesn't store this information for you (which I think is a good thing). -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Oct 16 2007
Kirk McDonald wrote:Janice Caron wrote:Another solution is to use a variant datatype which will take care of checking you're unboxing the right type. -- DanielSuppose I have union U { A a; B b; C c; } and a function f(U u) is there any way to determine at runtime, which union member has been assigned? I guess I want to do something like void f(U u) { if (is(u.a.typeof == A)) /* stuff */ else if (is(u.a.typeof == B)) /* stuff */ else if (is(u.a.typeof == C)) /* stuff */ } except obviously that won't work because typeof is a compile time thing, not a run time thing, and so it will always return A. I don't suppose there's a runtime solution? If not, I guess I'll just do what I've been doing all along (that is, store the choice in an enum outside the union).enum + union is the age-old solution to this. D doesn't store this information for you (which I think is a good thing).
Oct 16 2007
Reply to Janice,Suppose I have union U { A a; B b; C c; } and a function f(U u) is there any way to determine at runtime, which union member has been assigned? I guess I want to do something like void f(U u) { if (is(u.a.typeof == A)) /* stuff */ else if (is(u.a.typeof == B)) /* stuff */ else if (is(u.a.typeof == C)) /* stuff */ } except obviously that won't work because typeof is a compile time thing, not a run time thing, and so it will always return A. I don't suppose there's a runtime solution? If not, I guess I'll just do what I've been doing all along (that is, store the choice in an enum outside the union).if A, B and C are classes then you can uses (cast(A)(cast(Object)u.b) !is null) (cast(B)(cast(Object)u.c) !is null) (cast(C)(cast(Object)u.a) !is null) note the twist of the types. this is to force DMD to actually do the cast. It might work without it but might not.
Oct 16 2007
if A, B and C are classes then you can uses (cast(A)(cast(Object)u.b) !is null) (cast(B)(cast(Object)u.c) !is null) (cast(C)(cast(Object)u.a) !is null)If A, B, C are classes, you could also check with u.a.classinfo is A.classinfo u.a.classinfo is B.classinfo u.a.classinfo is C.classinfo. Christian
Oct 16 2007
On 10/16/07, Christian Kamm <kamm.incasoftware shift-at-left-and-remove-this.deIf A, B, C are classes, you could also check with u.a.classinfo is A.classinfo u.a.classinfo is B.classinfo u.a.classinfo is C.classinfo.That's really cool, but I can't find any documentation on .classinfo. I looked on the DigitalMars site in the pages "Classes", "Expressions" and "Properties", and after that I got stuck. Where is this documented?
Oct 16 2007
Reply to Janice,On 10/16/07, Christian Kamm <kamm.incasoftware shift-at-left-and-remove-this.delook under the phobos docsIf A, B, C are classes, you could also check with u.a.classinfo is A.classinfo u.a.classinfo is B.classinfo u.a.classinfo is C.classinfo.That's really cool, but I can't find any documentation on .classinfo. I looked on the DigitalMars site in the pages "Classes", "Expressions" and "Properties", and after that I got stuck. Where is this documented?
Oct 16 2007
BCS wrote:Reply to Janice,Specifically under "object", right at the top. There's a bunch of useful stuff documented in there. --bbOn 10/16/07, Christian Kamm <kamm.incasoftware shift-at-left-and-remove-this.delook under the phobos docsIf A, B, C are classes, you could also check with u.a.classinfo is A.classinfo u.a.classinfo is B.classinfo u.a.classinfo is C.classinfo.That's really cool, but I can't find any documentation on .classinfo. I looked on the DigitalMars site in the pages "Classes", "Expressions" and "Properties", and after that I got stuck. Where is this documented?
Oct 16 2007
Christian Kamm wrote:If A, B, C are classes, you could also check with u.a.classinfo is A.classinfo u.a.classinfo is B.classinfo u.a.classinfo is C.classinfo.Only if the union never stores references to subclasses.
Oct 16 2007
Frits van Bommel wrote:Christian Kamm wrote:Righto, AFAIK; in that case one might consider to have all classes implement an interface with a member that holds an identifier for the class. Or use, as has been mentioned already, the classical variant of adding the type info in a struct wrapped around the union. Regards, FrankIf A, B, C are classes, you could also check with u.a.classinfo is A.classinfo u.a.classinfo is B.classinfo u.a.classinfo is C.classinfo.Only if the union never stores references to subclasses.
Oct 16 2007