Scala - Import error

1

Good evening.

When I import a singleton object from 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

    
asked by 12jquiro 23.01.2017 в 03:16
source

1 answer

2

It is the error that you have not compiled the file ChecksumAccumulator.scala . When you divide the code into several files you can not continue using scala as a scripting language, you will have to learn how to compile with scalac or, more recommendable, with sbt .

Try with

$ scalac Summer.scala ChecksumAccumulator.scala
$ scala Summer
    
answered by 23.01.2017 / 22:05
source