Is it safe to use 'is' to compare types?
Yuxuan Shui via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Mar 3 16:22:20 PST 2016
On Thursday, 3 March 2016 at 23:58:39 UTC, 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.
Oh, I get it. 'a is b' works for the results of typeid(). But not
for duplicates of TypeInfo.
For example:
import std.stdio;
A a, b;
auto x = typeid(a), y = typeid(b);
writeln(x is y);
auto xz = ((cast(ubyte
*)x)[0..typeof(x).classinfo.init.length]).dup; //Evil
auto z = cast(typeof(x))(cast(void *)xz);
writeln(x is z); //false
writeln(x == z); //true
More information about the Digitalmars-d-learn
mailing list