Quiz of the day: Why does this not work?

Jarrett Billingsley jarrett.billingsley at gmail.com
Thu Nov 6 13:32:22 PST 2008


On Thu, Nov 6, 2008 at 4:23 PM, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
>
> "Sean Kelly" wrote
>> TomD wrote:
>>> Hi,
>>> this is about dmd and DLLs.
>>>
>>> Given a simple object hierachy in myclasses.d:
>>> module myclasses;
>>> class base{ char[] toString(){ return "I am base";} }
>>> class c1: base{ char[] toString(){ return "I am c1";} }
>>> class c2: base{ char[] toString(){ return "I am c2";} }
>> ...
>>>   assert( cast(c1) instances[1] !is null);
>>>   assert( cast(c2) instances[2] !is null);
>>>
>>>   return 0;
>>> }
>>
>> I think this is a bug in the runtime.  Look like 116 of:
>>
>> http://dsource.org/projects/tango/browser/trunk/lib/compiler/dmd/cast.d
>>
>> In your example, I'm pretty sure that the DLLs ClassInfo instance of c1
>> will be passed into _d_isbaseof2 while the ClassInfo obtained from the
>> object to be cast will come from the object's memory space (ie. from the
>> app).  Since an 'is' comparison is taking place and these are distinct
>> objects, the cast will fail.  Try changing the 'is' comparisons at lines
>> 105, 109, 116, 150, and 176 (I think that's all of them) to '==' and see
>> if that does the trick.
>
> Won't make a difference.  ClassInfo does not override the default opEquals,
> which does an is compare.

Even if it did, it's not really enough to do something like a name
compare to see if the two typeinfos are the same.  (Actually now that
I think about it, I think classinfo does that.. or did at some point
in the past.)  You would have to check and make sure that every piece
of the type - every member, every method, all the bases - were the
same in order for the types to be "equal".  This is obviously a
nontrivial operation and not something you want to have happen every
time you do a cast.

Having duplicated typeinfos is always bad news.  The best solution -
the one that DDLs and SOs provide - is the only sane, correct one: use
the same damn typeinfo for the same types.



More information about the Digitalmars-d mailing list