Is it safe to use 'is' to compare types?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Mar 4 07:18:55 PST 2016


On 3/3/16 6:58 PM, Yuxuan Shui wrote:
> On Thursday, 3 March 2016 at 23:51:16 UTC, Adam D. Ruppe wrote:
>> On Thursday, 3 March 2016 at 23:46:50 UTC, Yuxuan Shui wrote:
>>> Will typeid(a) is typeid(b) yield different results than typeid(a) ==
>>> typeid(b)?
>>
>> No. Indeed, opEquals on TypeInfo just calls is itself.
>
> But opEquals also has extra comparison:
>
>          auto ti = cast(const TypeInfo)o;
>          return ti && this.toString() == ti.toString();
>
> This makes me feel they are not the same.

In some cases, for instance using DLLs, the TypeInfo for an object 
allocated in one way may be identical, but be a different instance from 
the TypeInfo allocated in another way. This is why the string comparison 
occurs.

Note that comparing ANY object will first check if they are the same 
instance before calling any functions (this is in object.opEquals)

-Steve


More information about the Digitalmars-d-learn mailing list