scala/dpDc1.scala
// Base data
trait BaseD {
def eval: Int
def repr: String
def allT = (this, eval, repr)
def allS = this.toString + " =eval=> " + eval + " =repr=> " + repr
}
class NumD(val value: Int) extends BaseD {
def eval() = value
def repr() = value.toString
}
class NumPlus(val l: BaseD, val r: BaseD) extends BaseD {
def eval() = l.eval + r.eval
def repr() = '(' + l.repr + " + " + r.repr + ')'
}
val a = new NumD(12)
(a, a eval, a repr)
a.allT
a.allS
new NumPlus(a, new NumD(98)) allS
new NumPlus(new NumPlus(new NumD(12), new NumD(34)), new NumD(98)) allS
/*
// Base operation.
trait BaseOp {def computeNumD(data: NumD)}
// 1st concrete operation: evaluate the data
class EvalOp extends BaseOp {
var result: Option[Int] = _
def apply(data: BaseD) = {data.perform(this); result.get }
def computeNumD(data: NumD) {this.result = Some(data.value) }
}
// utility object for on-the-fly evaluations
object eval {def apply(data: BaseD) = new EvalOp()(data)}
*/