Feature request: interfaces declare types

Reiner Pope some at address.com
Wed May 23 14:49:50 PDT 2007


Henning Hasemann wrote:
> What I request is to be able to write something like:
> 
> interface ContainerIterator(T) {
>   T content();
> }
> 
> interface Container(T) {
>   type Iterator : ContainerIterator!(T);
>   
>   T first();
>   void remove(T);
>   Iterator(T) firstIterator();
>   void remove(Iterator(T));
> }


I'm not sure if this does what you want:

interface Container(T, Iterator)
{
     alias T Type;
     alias Iterator IteratorType;
     static assert(is(Iterator : ContainerIterator!(T)));
     T first();
     void remove(T);
     Iterator firstIterator();
     void remove(Iterator);
}

class ListIterator(T) : ContainerIterator!(T) { ... }

class LinkedList(T) : Container!(T, ListIterator!(T)) { ... }

> Usage example:
> 
> // I dont care what container they use
> class Foo {
>   Container(int) someNumbers;
>   // ...
>   int popNumber() {
>     Container(int).Iterator nr = someNumbers.firstIterator();
>     int r = nr.content();
>     someNumbers.remove(nr);
>     return r;
>   }
> }
> 

My code doesn't entirely suit your use-case, but it does something similar:

class Foo (ContainerType)  {
     ContainerType someNumbers;

     int popNumber() {
         ContainerType.Iterator nr = someNumbers.firstIterator();
         ...
     }
}

Your given usage example is a long way from D's type system: interfaces 
are dynamic in nature, so you would expect the Iterator type specified 
by it only to be known dynamically. But when you use types, you have to 
know them statically.

I think what you are after would be better solved by some kind of Concepts.

Cheers,

Reiner



More information about the Digitalmars-d mailing list