Goodnight.
When I import a singleton object of a class within the same folder, I get this error, look.
In this class called Summer.scala, is where I try to import the Singleton Object from the ChecksumAccumulator class
import ChecksumAccumulator.calculate
object Summer{
def main(args: Array[String]): Unit= {
for (arg <- args)
println(arg + " : " + calculate(arg))
}
}
ChecksumAccumulator class, with its Singleton Object ChecksumAccumulator
object ChecksumAccumulator {
/*
When a singleton object shares the same name
with a class, it is called that class's companion object.
*/
private val cache = mutable.Map.empty[String, Int]
def calculate(s: String): Int =
if (cache.contains(s))
cache(s)
else {
val acc = new ChecksumAccumulator
//The object with the same name of the class can acces to the private members.
//println(acc.sum)
for (c <- s)
acc.add(c.toByte)
val cs = acc.checksum()
cache += (s -> cs)
cs
}
def showMap() : Unit = {
for(base:String <- cache.keys)
println(cache(base))
}
//def showMap(): String = { var cadena = cache("Every value is an object.") + " "+ cache("Every value is an object. per second time!!"); cadena }
}
I get this error:
scala Summer.scala
Summer.scala:8: error: not found: object ChecksumAccumulator import ChecksumAccumulator.calculate ^ Summer.scala:14: error: not found: value calculate println(arg + " : " + calculate(arg)) ^ two errors found
It is the error that you have not compiled the
ChecksumAccumulator.scala
. When you split your code into multiple files you can't still use scala as your scripting language, you'll have to learn how to compile withscalac
or, more recommendably, withsbt
.Test