Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot compile file in Scala

Tags:

scala

From an example in book "Begining in Scala", the script is:

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.toByte)
     val cs=acc.checksum
      cache+= (s -> cs)
     cs
  }
}

but, when I tried to compile this file $scalac ChecksumAccumulator.scala, then generate an error, "not found: type ChecksumAccumulator val acc = new ChecksumAccumulator", any suggest?

Thanks,

like image 402
william luo Avatar asked Dec 07 '22 17:12

william luo


1 Answers

'object' keyword defines a singleton object, not a class. So you can't new an object, the 'new' keyword requires a class.

check this Difference between object and class in Scala

like image 119
Zang MingJie Avatar answered Dec 26 '22 21:12

Zang MingJie