www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Interface Covariance Bug?

reply "John C" <johnch_atms hotmail.com> writes:
This compiles, but produces some strange output on the cmd line (DMD 0.150, 
Windows XP SP2):

interface ICollection(T) {
  int length();
}

interface IMap(TKey, TValue) {
  ICollection!(TKey) keys();
}

class Map(TKey, TValue) : IMap!(TKey, TValue) {
  KeyCollection keys() {
    return new KeyCollection;
  }
  class KeyCollection : ICollection!(TKey) {
    int length() {
      return 5;
    }
  }
}

void main() {
    IMap!(int, int) map = new Map!(int, int);
    writefln(map.keys.length);
}

Output:
KeyCollection
10227584

Why does it output "KeyCollection"? Is it some kind of runtime error? And 
why is map.keys.length not 5? 
Mar 26 2006
parent reply "Walter Bright" <newshound digitalmars.com> writes:
It's a forward reference problem. Try reversing the declarations of keys() 
and KeyCollection. 
Mar 27 2006
parent reply "John C" <johnch_atms hotmail.com> writes:
"Walter Bright" <newshound digitalmars.com> wrote in message 
news:e09vmu$2ubc$1 digitaldaemon.com...
 It's a forward reference problem. Try reversing the declarations of keys() 
 and KeyCollection.

Thanks - that fixed it. But surely it highlights a separate bug: forward referencing isn't supposed to be an issue in D, is it? John.
Mar 28 2006
next sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
John C wrote:
 "Walter Bright" <newshound digitalmars.com> wrote in message 
 news:e09vmu$2ubc$1 digitaldaemon.com...
 It's a forward reference problem. Try reversing the declarations of keys() 
 and KeyCollection.

Thanks - that fixed it. But surely it highlights a separate bug: forward referencing isn't supposed to be an issue in D, is it?

Correct. You'll notice that many forward reference bugs have been filed both here and in DStress. But a forward reference bug that leads to bad code generation is a new one on me. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Mar 28 2006
prev sibling next sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
John C wrote:
 "Walter Bright" <newshound digitalmars.com> wrote in message 
 news:e09vmu$2ubc$1 digitaldaemon.com...
 It's a forward reference problem. Try reversing the declarations of keys() 
 and KeyCollection.

Thanks - that fixed it. But surely it highlights a separate bug: forward referencing isn't supposed to be an issue in D, is it?

Looking at your code again, it appears to be a case of http://d.puremagic.com/bugzilla/show_bug.cgi?id=65 I've just noticed that my testcase contains a forward reference as well. Guess I'll have to try rewriting it without the forward reference and see what happens. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Mar 28 2006
prev sibling parent "Walter Bright" <newshound digitalmars.com> writes:
"John C" <johnch_atms hotmail.com> wrote in message 
news:e0avij$1bpj$1 digitaldaemon.com...
 But surely it highlights a separate bug: forward referencing isn't 
 supposed to be an issue in D, is it?

Generally, it isn't. But the internal semantic routines need to be reorganized/reimplemented to do this better, and I'm not up for that right now.
Mar 28 2006