Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
March 30, 2020 what is the different bettwen typeid and .classinfo? | ||||
---|---|---|---|---|
| ||||
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 == ``` |
March 30, 2020 Re: what is the different bettwen typeid and .classinfo? | ||||
---|---|---|---|---|
| ||||
Posted in reply to lilijreey | 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
|
March 30, 2020 Re: what is the different bettwen typeid and .classinfo? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 30 March 2020 at 15:15:08 UTC, Steven Schveighoffer wrote:
> 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
Will it be the same if using "is", the reason is for function that use "nothrow" attribute?
if (cc.classinfo is typeid(CC))
|
March 30, 2020 Re: what is the different bettwen typeid and .classinfo? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pham | On 3/30/20 1:06 PM, Pham wrote:
> On Monday, 30 March 2020 at 15:15:08 UTC, Steven Schveighoffer wrote:
>> 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).
>>
>
> Will it be the same if using "is", the reason is for function that use "nothrow" attribute?
> if (cc.classinfo is typeid(CC))
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".
-Steve
|
March 30, 2020 Re: what is the different bettwen typeid and .classinfo? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 30 March 2020 at 18:20:00 UTC, Steven Schveighoffer wrote: > On 3/30/20 1:06 PM, Pham wrote: >> >> 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 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(); } |
March 30, 2020 Re: what is the different bettwen typeid and .classinfo? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pham | On 3/30/20 5:17 PM, Pham wrote: > On Monday, 30 March 2020 at 18:20:00 UTC, Steven Schveighoffer wrote: >> I 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(); > } 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#.opEquals |
Copyright © 1999-2021 by the D Language Foundation