The Artima Developer Community


Errata for Programming in Scala, 2nd Edition
Chapter 20: Abstract Members
Return to errata index.

Page 411 (PDF page 447):
Minor quibble. Book code gives this

  trait Abstract {
    type T
    def transform(x: T): T
    val initial: T
    var current: T
  }

and a class instantion of this

  class Concrete extends Abstract {
    type T = String
    def transform(x: String) = x + x
    val initial = "hi"
    var current = initial
  }

Note this in the trait 

    def transform(x: T): T

is given as this in the concrete instatiation

    def transform(x: String) = x + x

whereas logically (I think) it should minimise any changes to the
concrete and rely directly on the type alias, hence the above should be 

    def transform(x: T) = x + x

I've tested it and it compiles.
Page 425 (PDF page 461):
It makes sense that I cannot feed the Cow 'bessy' a new Fish, but I
cannot feed bessy new Grass either.  From the text it seems that I should
be able to.  Why else point out that bessy cannot eat Fish if it can't
eat Grass either?

scala> class Food
defined class Food

scala> abstract class Animal {
     | type SuitableFood <: Food
     | def eat(food: SuitableFood)
     | }
defined class Animal

scala> class Grass extends Food
defined class Grass

scala> class Cow extends Animal {
     | type SuitableFood = Grass
     | override def eat(food: Grass) {}
     | }
defined class Cow

scala> class Fish extends Food
defined class Fish

scala> val bessy: Animal = new Cow
bessy: Animal = Cow@4bd767

scala> bessy eat (new Fish)
<console>:12: error: type mismatch;
 found   : Fish
 required: bessy.SuitableFood
       bessy eat (new Fish)
                  ^

scala> bessy eat (new Grass)
<console>:11: error: type mismatch;
 found   : Grass
 required: bessy.SuitableFood
       bessy eat (new Grass)
                  ^
Page 438 (PDF page 474):
The decimals function gives a StackOverflowError for numbers between 2
and 9:

scala> def decimals(n: Long): Int = 
     | if (n == 1) 0 else 1 + decimals(n / 10)
decimals: (n: Long)Int

scala> decimals(100)
res4: Int = 2

scala> decimals(101)
res5: Int = 2

scala> decimals(1)  
res6: Int = 0

scala> decimals(5)
java.lang.StackOverflowError
	at .decimals(<console>:6)
	at .decimals(<console>:6)
	at .decimals(<console>:6)
	at .decimals(<console>:6)
	at .decimals(<console>:6)
	at .decimals(<console>:6)
	at .decimals(<console>:6)
	at .decimals(<console>:6)
	at .decimals(<console>:6)
	at .decimals(<console>:6)
	at .decimals(<console>:6)

Page number: Book type: Paperback book PDF eBook
Book version: (Or build date. Found on back of title page.)
Your feedback:
Your name: (optional)
Your email address: (optional) (will not be published)

Copyright © 2021 Artima, Inc. All rights reserved.