I have PrePrint Edition Version 4 and am trying the examples with Scala 2.7.2.RC3 and am having lots of problems with the Element subclasses in Chapter 10.
1. LineElement does not compile: E:\Data\Scala\ProgInScala\Ch10>fsc *.scala E:\Data\Scala\ProgInScala\Ch10\LineElement.scala:2: error: error overriding value width in class Element of type Int; method width needs to be an immutable value override def width = s.length ^ E:\Data\Scala\ProgInScala\Ch10\LineElement.scala:3: error: error overriding value height in class Element of type Int; method height needs to be an immutable value override def height = 1 ^ two errors found
To make progress, I changed the "def"s to "val"s and the compiler was happy.
2. Instantiating a UniformElement throws a NullPointer Exception: scala> val e3: Element = new UniformElement('x', 2, 3) java.lang.NullPointerException at Element.<init>(Element.scala:5) at UniformElement.<init>(UniformElement.scala:5) at .<init>(<console>:4) at .<clinit>(<console>) at RequestResult$.<init>(<console>:3) at RequestResult$.<clinit>(<console>) at RequestResult$result(<console>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.i...
Investigating, I found that "private val line = ch.toString * width" gives line == null which leads to contents == null. Changing the "val" to "def" makes it work.
I find two problems related to val and def somehow suspicious. I have the same problems with Scala.2.7.1.final and with the Scala plug-in for eclipse.
I use Windows XP Profesional SP3 with all updates and Java 1.6.0_07.
I am sorry. When trying this, I accidentally substituted the hypothetical definition of Element at the bottom of page 213 for the real definition in Listing 10.2 at the top of the page. This the cause of problem 1.
The solution to problem 2 is the same as to problem 1. The definition of Element with def's for width and height works because Element.<init> does not reference contents. The definition of Element with val's for width and height has a problem with the subclass UniformElement because Element.<init> need contents which references UniformElement.line, which is not initialized until after the call to Element.<init>.
It was all a stupid mistake. But, I learned a lot.