Selftype: an idea from Scala for D2.0

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Fri May 25 13:30:11 PDT 2007


eao197 wrote:

> Hi!
> 
> There is an interesting article Scalable Component Abstractions
> (http://lamp.epfl.ch/~odersky/papers/ScalableComponent.pdf) which
> describes some features of Scala language (http://www.scala-lang.org) for
> building scalable software components. Two of these features, abstract
> members and selftypes, are illustrated by the following example of
> Subject/Observer pattern implementation:
> 
> abstract class SubjectObserver {
>    type S <: Subject;
>    type O <: Observer;
>    abstract class Subject requires S { /* (1) */
>      private var observers: List[O]  List();
>      def subscribe(obs: O)  /* (2) */
>         observers  obs :: observers;
>      def publish         for (val obs <observers)
>            obs.notify(this);  /* (3) */
>    }
> 
>    abstract class Observer {
>      def notify(sub: S): unit; /* (4) */
>    }
> }

I can see how this removes the need for "unsafe" casts. However, it might
cause some extra trouble to the planned reflection support :) From what
I've read and heard, Scala does not yet support reflection.

Can you show how this would work if there were multiple subclasses
of 'Subject' and 'Observer'? (for example multiple sensor types in the 
SensorReader example) Does it require language support for multiple
dispatch then?




More information about the Digitalmars-d mailing list