digitalmars.D.learn - what is the different bettwen typeid and .classinfo?
- lilijreey (17/17) Mar 30 2020 Hi:
- Steven Schveighoffer (6/28) Mar 30 2020 They are both the same. Historically they were different (ClassInfo was
- Pham (5/33) Mar 30 2020 Will it be the same if using "is", the reason is for function
- Steven Schveighoffer (23/33) Mar 30 2020 It should be identical.
- Pham (17/24) Mar 30 2020 void main()
- Steven Schveighoffer (14/33) Mar 30 2020 In this case, the difference is == vs. `is`, not classinfo vs typeid:
Hi: I write code like this ```D class Base { int base; } class CC :Base { int cc; } auto cc = new CC; //I want check cc object type is CC if (typeid(cc) == typeid(CC)) // ok == if (cc.classinfo == AstAssignNode) //error if (cc.classinfo == AstAssignNode.classinfo) // ok == ```
Mar 30 2020
On 3/30/20 10:38 AM, lilijreey wrote:Hi: I write code like this ```D class Base { int base; } class CC :Base { int cc; } auto cc = new CC; //I want check cc object type is CC if (typeid(cc) == typeid(CC)) // ok == if (cc.classinfo == AstAssignNode) //error if (cc.classinfo == AstAssignNode.classinfo) // ok == ```They are both the same. Historically they were different (ClassInfo was its own type different from TypeInfo_Class). I would recommend typeid for future-proof code (it's possible, however unlikely, that .classinfo at some point goes away). -Steve
Mar 30 2020
On Monday, 30 March 2020 at 15:15:08 UTC, Steven Schveighoffer wrote:On 3/30/20 10:38 AM, lilijreey wrote:Will it be the same if using "is", the reason is for function that use "nothrow" attribute? if (cc.classinfo is typeid(CC))Hi: I write code like this ```D class Base { int base; } class CC :Base { int cc; } auto cc = new CC; //I want check cc object type is CC if (typeid(cc) == typeid(CC)) // ok == if (cc.classinfo == AstAssignNode) //error if (cc.classinfo == AstAssignNode.classinfo) // ok == ```They are both the same. Historically they were different (ClassInfo was its own type different from TypeInfo_Class). I would recommend typeid for future-proof code (it's possible, however unlikely, that .classinfo at some point goes away). -Steve
Mar 30 2020
On 3/30/20 1:06 PM, Pham wrote:On Monday, 30 March 2020 at 15:15:08 UTC, Steven Schveighoffer wrote:It should be identical. For fun I did an AST dump for this code: void main() { Object o; auto ti = typeid(o); auto ci = o.classinfo; } And this is what I get: void main() { Object o = null; TypeInfo_Class ti = typeid(o); TypeInfo_Class ci = **o; return 0; } So it looks like typeid(o) is not lowered to something, but o.classinfo is lowered to **o (the first field in an object is the TypeInfo). I would say that this is going to reduce to the same thing in both cases, but I find it interesting that the AST is different. I don't really understand the question about "nothrow". -SteveThey are both the same. Historically they were different (ClassInfo was its own type different from TypeInfo_Class). I would recommend typeid for future-proof code (it's possible, however unlikely, that .classinfo at some point goes away).Will it be the same if using "is", the reason is for function that use "nothrow" attribute? if (cc.classinfo is typeid(CC))
Mar 30 2020
On Monday, 30 March 2020 at 18:20:00 UTC, Steven Schveighoffer wrote:On 3/30/20 1:06 PM, Pham wrote:void main() { static class Foo { int i; } static bool checkFoo() nothrow { // error when compile //return typeid(Foo) == Foo.classinfo; // ok when compile return typeid(Foo) is Foo.classinfo; } checkFoo(); }Will it be the same if using "is", the reason is for function that use "nothrow" attribute? if (cc.classinfo is typeid(CC))I don't really understand the question about "nothrow". -Steve
Mar 30 2020
On 3/30/20 5:17 PM, Pham wrote:On Monday, 30 March 2020 at 18:20:00 UTC, Steven Schveighoffer wrote:In this case, the difference is == vs. `is`, not classinfo vs typeid: typeid(Foo) == typeid(Foo); // throws Foo.classinfo == Foo.classinfo; // throws typeid(Foo) is typeid(Foo); // nothrow Foo.classinfo is Foo.classinfo; // nothrow `is` between class instances means "is this the same instance". It is a simple comparison of two pointers basically. == between classes means calling object.opEquals [1], which is not nothrow (or nogc, or pure). In fact, they should be the same instance, because typeid and classinfo do the same thing. -Steve [1] https://dlang.org/phobos/object.html#.opEqualsI don't really understand the question about "nothrow".void main() { static class Foo { int i; } static bool checkFoo() nothrow { // error when compile //return typeid(Foo) == Foo.classinfo; // ok when compile return typeid(Foo) is Foo.classinfo; } checkFoo(); }
Mar 30 2020