Selftype: an idea from Scala for D2.0

eao197 eao197 at intervale.ru
Tue May 29 07:55:44 PDT 2007


On Sat, 26 May 2007 00:30:11 +0400, Jari-Matti Mäkelä  
<jmjmak at utu.fi.invalid> wrote:

> 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 complex example is here:  
http://eao197.narod.ru/scala/AbstractTypesDemo.scala.html (thanks to  
aka50!)
I've add yet another descendant of Sensor -- CalibrateableSensor with can  
use Calibrator for adjusting his values. Display can show values from  
Sensor and from CalibrateableSensor, but Calibrator can work only with  
CalibrateableSensor.

Here is some difficalty in extending classes with selftypes by inheritance  
in Scala. It is impossible to simple inherit CalbrateableSensor from  
Sensor and add data necessary for calibration support to it. That is  
because it is necessary to define additional trait CalibrateableEntity and  
use it in 'requires' clause for CalibrateableSensor trait.

In D construct 'class A is B' for classes in template may be more useful  
and less restricted:

template SubjectObserver(S,O) {
   class Subject is S { ... }
   class Observer { ... }
}

template AbstractSensorReader(S,D) {
   class Sensor is S : SubjectObserver!(Sensor, D).Subject { ... }
   class Display : SubjectObserver!(S, D).Observer { ... }

   class CalibrateableSensor : Sensor { ... }
   class Calibrator { ... }
}

without need of additional interfaces an so on.

-- 
Regards,
Yauheni Akhotnikau



More information about the Digitalmars-d mailing list