Selftype: an idea from Scala for D2.0

eao197 eao197 at intervale.ru
Fri May 25 11:52:09 PDT 2007


On Fri, 25 May 2007 21:48:10 +0400, Henning Hasemann <hhasemann at web.de>  
wrote:

> I must confess I didnt read your whole posting to the end,
> but it seems that this is much the same idea as I had in
> "Feature Request: interfaces declare types" plus some extra
> intelligence, or?

As I can see your idea is very close to 'abstract members' in Scala:

class Demo {
   type A // This member must be defined in a derived class.
             // There isn't any restruction to type A.

   type B <: Another // This is another member which must be
                             // defined in a derived class.

   /* ... In the body of class Demo names A and B can be
           used as template parameters in D templates ... */
}

This looks similar to your proposal:

interface ContainerIterator(T) {
   T content();
}

interface Container(T) {
   type Iterator : ContainerIterator!(T); // Like Scala's 'type Iterator <:  
ContainerIterator[T]'

   T first();
   void remove(T);
   Iterator(T) firstIterator();
   void remove(Iterator(T));
}

JFYI: There is a citation from 'Scalable Component Abstractions':
\begin{quote}
...
Abstract type members provide a fexible way to abstract over concrete  
types of components. Abstract types can hide information about internals  
of a component, similar to their use in SML signatures. In an  
object-oriented framework where classes can be extended by inheritance,  
they may also be used as a fexible means of parameterization (often called  
family polymorphism [11]).
...
[11] E. Ernst. Family polymorphism. In Proceedings of the European  
Conference on Object-Oriented Programming, pages 303-326, Budapest,  
Hungary, 2001.
\end{quote}

So it seems that you reinvent the idea about 'abstract type members' ;)

But I speak about another Scala's feature: selftypes. For example:

template Demo(S) {
   class Dumpable {
     void dump(S stream) {
       stream.put( this ); // At this point 'this' is MyClass.
     }
   }
}

My proposal is to tell the compiler that 'this' in MyClass has type T or  
any of its subclass:

template Demo(S, T) {
   class Dumpable is T {
     void dump(S stream) {
       stream.put( this ); // At this point 'this' is T.
     }
   }
}

class MyStream {
   void put( MyClass obj ) { ... }
}

class MyClass : Demo!( MyStream, MyClass ).Dumpable {
   ...
}

auto myStream = new MyStream;
auto my = new MyClass;
my.dump( myStream );  // Method MyStream.put(MyClass) must be called.

-- 
Regards,
Yauheni Akhotnikau



More information about the Digitalmars-d mailing list