@@ -7,10 +7,10 @@ trait Text {
7
7
def apply (idx : Int ): Char
8
8
def concat (txt : Text ): Text
9
9
def toStr : String
10
+ def flatten = `common`.fromString(toStr)
10
11
}
11
12
object Text {
12
13
trait Common { self =>
13
- type Instance <: Text { val `common` : self.type }
14
14
def fromString (str : String ): Text
15
15
def fromStrings (strs : String * ): Text =
16
16
(" " :: strs.toList).map(fromString).reduceLeft(_.concat(_))
@@ -19,13 +19,14 @@ object Text {
19
19
20
20
class FlatText (str : String ) extends Text {
21
21
val common : FlatText .type = FlatText
22
+ import `common` ._
23
+
22
24
def length = str.length
23
25
def apply (n : Int ) = str.charAt(n)
24
26
def concat (txt : Text ) = new FlatText (str ++ txt.toStr)
25
27
def toStr = str
26
28
}
27
29
object FlatText extends Text .Common {
28
- type Instance = FlatText
29
30
def fromString (str : String ) = new FlatText (str)
30
31
}
31
32
@@ -45,7 +46,7 @@ enum ConcText extends Text {
45
46
case Conc (t1, t2) => if (n < t1.length) t1(n) else t2(n - t1.length)
46
47
}
47
48
48
- def concat (txt : Text ) = Conc (this , txt)
49
+ def concat (txt : Text ): Text = Conc (this , txt)
49
50
50
51
def toStr : String = this match {
51
52
case Str (s) => s
@@ -54,7 +55,6 @@ enum ConcText extends Text {
54
55
}
55
56
56
57
object ConcText extends Text .Common {
57
- type Instance = ConcText
58
58
def fromString (str : String ) = Str (str)
59
59
}
60
60
0 commit comments