www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - AA with an keytype of interface segfaults

reply brad beveridge <brad nowhere.com> writes:
The following code segfaults, it appears that you cannot use an 
interface as the key for an associative array.

interface foo
{
     void bar();
}

class MyClass : foo
{
     void bar() {};
}

void main ()
{
     int [foo] c;
     MyClass mc = new MyClass ();
     c[mc] = 0;
}

Brad
Feb 28 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
brad beveridge wrote:

 The following code segfaults, it appears that you cannot use an 
 interface as the key for an associative array.
 
 interface foo
 {
     void bar();
 }
 
 class MyClass : foo
 {
     void bar() {};
 }
 
 void main ()
 {
     int [foo] c;
     MyClass mc = new MyClass ();
     c[mc] = 0;
 } 
Seems to be related to calling toHash() on an interface ? (side note: on Mac, I get "illegal hardware instruction") std.typeinfo.ti_C.d:
 class TypeInfo_C : TypeInfo
     uint getHash(void *p)
     {
 	Object o = *cast(Object*)p;
 	assert(o);
 	return o.toHash();
     }
Because it crashes, even without involving the AA type : (The actual crash seems to occur in object.Object.print ?)
 void main ()
 {
     MyClass mc = new MyClass ();
     foo mi = mc;
     Object* mp = cast(Object*)&mi;
     Object mo = *mp;
     debug printf("%d\n", mo.toHash());
 } 
If one uses the class instead of the interface, it works...
 void main ()
 {
     MyClass mc = new MyClass ();
     Object* mp = cast(Object*)&mc;
     Object mo = *mp;
     debug printf("%d\n", mo.toHash());
 } 
--anders PS. Bugs go in the bugs group.
Feb 28 2005
parent brad beveridge <brad nowhere.com> writes:
 PS. Bugs go in the bugs group.
Doh! I'll try to remember next time. Thanks for posting to there also. Brad
Feb 28 2005