Associative array bug?

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Sun Jun 18 07:06:55 PDT 2006


Max Samuha wrote:
> Can interfaces be used as AA keys? The following code causes access
> violation (WinXP, dmd 0.160):
> 
> interface I
> {
> 	void method();
> }
> 
> class A : I
> {
> 	void method()
> 	{
> 	}
> }
> 
> void main()
> {
> 	I a = new A();
> 	
> 	int[I] array;
> 
> 	array[a] = 5; // Access violation	
> }
> 
> When I replace the interface with a base class, it works.
> 
> abstract class I
> {
> 	void method();
> }
> 
> class A : I
> {
> 	override void method()
> 	{
> 	}
> }
> 
> void main()
> {
> 	I a = new A();
> 	
> 	int[I] array;
> 
> 	array[a] = 5; // Ok	
> }
> 
> Is it a known bug?

It's a bug for sure, regardless of whether interfaces can be used as AA 
keys. (If it is allowed, then the program shouldn't crash, if it isn't 
allowed the program shouldn't compile)

If it is known, I dunno, it's just a matter of searching the bugzilla.

----

Now, whether interfaces can be used as AA keys or not, it depends if we 
have a method to do hash from an interface. One way would be to require 
the interface to have a toHash method. But I'm not sure if that would 
make much sense.
Another way, perhaps the most sensible, would be to cast to Object and 
use the underlying toHash(). But that depends on whether it is allowed 
that an interface can be converted to a reference to it's underlying class.
The doc only says "A class that implements an interface can be converted 
to a reference to that interface.", but not the otherwise. However it 
seems that DMD does allow for an interface to be converted to a class 
(since that feature is what allows interface->class return type 
covariance to work), but that is all very not explicit and not documented.




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



More information about the Digitalmars-d-learn mailing list