I’m reading ‘Programming in scala’ and in one of the example when I try to compile it in Eclipse I receive error : ‘not found: type ChecksumAccumulator’ The type is declared as below. Is the code below correct ?
import scala.collection.mutable.Map
object ChecksumAccumulator {
private val cache = Map[String, Int]()
def calculate(s: String): Int =
if(cache.contains(s))
cache(s)
else {
val acc = new ChecksumAccumulator
for(c <- s)
acc.add(c.toBye)
val cs = acc.checksum()
cache += (s -> cs)
cs
}
}
From Programming in Scala:
If you try to compile this code alone, without the
ChecksumAccumulatorclass, you’ll get a compiler error because you can’t create an instance of a singleton object withnew.The book does a great job of explaining how companion objects and classes work together, and since you’re already reading it I won’t bother adding any further summary here.