blob: f3d4a966bb831a2736f6208cb5b76dad005c9fc0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package expAbstractData;
/** A base class consisting of
* - a root trait (i.e. abstract class) `Exp' with an `eval' function
* - an abstract type `exp' bounded by `Exp'
* - a concrete instance class `Num' of `Exp' for numeric literals
*/
trait Base {
type exp <: Exp;
trait Exp {
def eval: int
}
class Num(v: int): exp extends Exp {
val value = v;
def eval = value
}
}
object testBase extends Base with Executable {
type exp = Exp;
val term = new Num(2);
System.out.println(term.eval);
}
/** Data extension: An extension of `Base' with `Plus' expressions
*/
trait BasePlus extends Base {
class Plus(l: exp, r: exp): exp extends Exp {
val left = l;
val right = r;
def eval = left.eval + right.eval
}
}
/** Operation extension: An extension of `Base' with 'show' methods.
*/
trait Show extends Base {
type exp <: Exp1;
trait Exp1 extends Exp {
def show: String;
}
class Num1(v: int): exp extends Num(v) with Exp1 {
def show = value.toString();
}
}
/** Operation extension: An extension of `BasePlus' with 'show' methods.
*/
trait ShowPlus extends BasePlus with Show {
class Plus1(l: exp, r: exp): exp extends Plus(l, r) with Exp1 {
def show = left.show + " + " + right.show
}
}
|