typeinfo comparison - 'is' or '=='?

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Mon May 8 14:52:54 PDT 2006


Bruno Medeiros wrote:
> Anders F Björklund wrote:
>> Gregor Richards wrote:
>>
>>> I've ran into an issue with typeinfo objects.  It seems that for any 
>>> type, there should be a static object containing the type info, and 
>>> furthermore there should only be one in the output program.  My 
>>> reasoning behind this is that several .d files in phobos/ use 'is' to 
>>> compare typeinfo's, which implies to me that the 'typeid' operator 
>>> will return a reference to a statically declared type (since 
>>> otherwise it could return a different value and you'd get false 
>>> negatives)
>>
>> This is a problem for GDC too, when running on old compilers (GCC 3.3) 
>> that doesn't support once-only linkage and therefore duplicates objects.
>>
>> http://www.digitalmars.com/d/archives/D/gnu/1594.html
>>
>>> So my question is: Am I wrong, or is the code in phobos/std/boxer.d 
>>> and phobos/std/stream.d wrong?
>>
>> Well, not "wrong" - it is just not portable ? But changing it from 'is'
>> over to use '==' instead would make it work in more places than it does.
>>
>> So I think that Phobos should be changed.
>>
>> --anders
> 
> Whoa there, I just realized: This is a global language issue, it is not 
> just a matter of Phobos. It has to be defined globally (by the language 
> itself) what is the correct way to compare two types.
> I mean, what if in a D program one uses == / is with a TypeInfo? 
> Wouldn't GDC support == ?
> 

Hum, actually, after some tests, and a very brief look in Phobos, it 
seems there are two kinds of comparisons available, each one 
corresponding to each operator.
"is" tests for exact type equality, and
"==" tests for meta-type equality (aka archetype, type type, super type?)
Example:

   writefln(typeid(FooBar) is typeid(Foo));  //false
   writefln(typeid(FooBar) == typeid(int*)); //false
   writefln(typeid(FooBar) == typeid(Foo)); //true,same metatype(class)
   writefln(typeid(char*) == typeid(int*)); //true,same metatype(pointer)

Then there's also the equals method of TypeInfo, and that I didn't quite 
understand what it does (or when). It seems to me redundant versus opEquals.
In any case, it seems both operands must support their respective usage. 
We could use some official documentation on this too, as there's quite 
no mention of it anywhere, I think.


-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list