Martin Odersky による本 Programming in Scala (second edition) を読んでいますが、第 10 章の例に問題があります。
これは、章のほぼ最後にある私のファイルです。
class Element
object Element {
private class ArrayElement(
val contents: Array[String]
) extends Element
private class LineElement(s: String) extends ArrayElement(Array(s)) {
override def width = s.length
override def height = 1
}
private class UniformElement(
ch: Char,
override val width: Int,
override val height: Int
) extends Element {
private val line = ch.toString * width
def contents = Array.fill(height)(line)
}
def elem(contents: Array[String]): Element =
new ArrayElement(contents)
def elem(chr: Char, width: Int, height: Int): Element =
new UniformElement(chr, width, height)
def elem(line: String): Element =
new LineElement(line)
}
abstract class Element {
def contents: Array[String]
def width: Int =
if (height == 0) 0 else contents(0).length
def height: Int = contents.length
def above(that: Element): Element =
elem(this.contents ++ that.contents)
def beside(that: Element): Element =
elem(
for (
(line1, line2) <- this.contents zip that.contents
) yield line1 + line2
)
}
コンパイラは次のように言っています。
defined class Element
<console>:15: error: method width overrides nothing
override def width = s.length
^
<console>:16: error: method height overrides nothing
override def height = 1
^
<console>:21: error: value width overrides nothing
override val width: Int,
^
<console>:22: error: value height overrides nothing
override val height: Int
^
<console>:17: error: not found: value elem
elem(this.contents ++ that.contents)
^
<console>:20: error: not found: value elem
elem(
^
を最初から削除するclass Element
と、サブクラス化しようとすると Element タイプが見つからないというエラーが表示されます。
ここで、本のこの章について既に議論しているいくつかのトピックを見つけましたが、そこで提案された解決策を使用できませんでした。
私は何を取りこぼしたか?
よろしく、ノーバート