www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - TypeInfo of arrays of basic types.

reply "Eldar Insafutdinov" <e.insafutdinov gmail.com> writes:
I was playing around with Variant and came across a very annoying 
problem:

     auto str_arr = Variant(["1", "2", "3"]);
     assert(cast(TypeInfo_Array)str_arr.type(), "This is fine");
     auto int_arr = Variant([1, 2, 3]);
     assert(cast(TypeInfo_Array)int_arr.type(), "This is broken");

While TypeInfo object of string[] is a TypeInfo_Array instance, 
TypeInfo of int[] isn't. Much to my surprise I found out that 
TypeInfo of int[] is a mysterious class TypeInfo_Ai and is 
actually a subclass of TypeInfo_Class:

     writefln("typeinfo of int[]: %s", 
typeid(int[]).classinfo.toString());
     writefln("base class of typeinfo int[] %s", 
typeid(int[]).classinfo.base.classinfo);

     // Outputs
     typeinfo of int[]: TypeInfo_Ai
     base class of typeinfo int[]: TypeInfo_Class

Googling didn't yield any relevant discussions, so I posted it 
here, although I'm pretty sure someone should have come across 
the problem in the past. Is this a fundamental issue with the 
runtime and compiler or is it a bug that can be fixed without 
breaking too much?
Mar 31 2012
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sun, Apr 01, 2012 at 02:08:04AM +0200, Eldar Insafutdinov wrote:
 I was playing around with Variant and came across a very annoying
 problem:
 
     auto str_arr = Variant(["1", "2", "3"]);
     assert(cast(TypeInfo_Array)str_arr.type(), "This is fine");
     auto int_arr = Variant([1, 2, 3]);
     assert(cast(TypeInfo_Array)int_arr.type(), "This is broken");
 
 While TypeInfo object of string[] is a TypeInfo_Array instance,
 TypeInfo of int[] isn't. Much to my surprise I found out that
 TypeInfo of int[] is a mysterious class TypeInfo_Ai and is actually
 a subclass of TypeInfo_Class:

I've seen this before. TypeInfo_Ai is apparently some kind of built-in typeinfo that's handled separately from TypeInfo_Array. Many arrays of built-in types have their own typeinfo's this way; it seems TypeInfo_Array is only used in the more general case. I don't know compiler internals to know why things are this way, but I've definitely seen problems caused by this split between specific TI's and general TI's (e.g., the fact that getHash() of char[] and immutable(char)[] (i.e., string) is inconsistent with the getHash() of const(char)[]). T -- Valentine's Day: an occasion for florists to reach into the wallets of nominal lovers in dire need of being reminded to profess their hypothetical love for their long-forgotten.
Mar 31 2012