0

I'm learning Scala, and I was attempting to import from 2 scrips in different classes, but, I get an error, and I don't know why.

This class, in Summer.scala, is where I try to import the singleton object from class ChecksumAccumulator.

import ChecksumAccumulator.calculate

object Summer{
  def main(args: Array[String]): Unit= {
    for (arg <- args)
      println(arg + " : " +  calculate(arg))
  }
}

Class ChecksumAccumulator, with the 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  }
}

This script throw this mistake

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

Jquiro12
  • 3
  • 3

1 Answers1

0

It seems like it's a duplicated question, but I can't yet mark questions as duplicated nor comment on them.

If that is all your code, it means you're missing:

class ChecksumAccumulator { ... }

Because you're trying to create an instance of this class:

val acc = new ChecksumAccumulator

And it's impossible to achieve with just:

object ChecksumAccumulator { ... }

Check out this answer about exactly the same code example. There is the missing definition of class ChecksumAccumulator :

https://stackoverflow.com/a/8681616/5100014

Just make sure to paste it into the same file with object ChecksumAccumulator

Community
  • 1
  • 1
zhelezoglo
  • 212
  • 2
  • 11
  • Ok, firts, my ask is different to the other Question, Second, I don't have problems with the class class ChecksumAccumulator { ... }, It run perfect, the Object has the same name of the class, and It's pissible to do in scala, My problem is when I want to import 1 Scala script to another Scala Scrip. The ChecksumAccumulator Script run in a good way. – Jquiro12 Jan 23 '17 at 14:48
  • How are you running that? From IDE or from REPL? How do you define ChecksumAccumulator object and class? In one file or in REPL? – zhelezoglo Jan 23 '17 at 16:41
  • It run from REPL, and, I define ChecksumAccumulator object and class in one File – Jquiro12 Jan 23 '17 at 23:01
  • I asked about REPL, because one should be careful defining companion objects in standard Scala REPL. You can check if you want: http://stackoverflow.com/a/29554506/5100014 . But if you define them in a file, it's probably not your issue. I think your REPL doesn't see that file. Maybe first you need to load it. See http://stackoverflow.com/a/7385961/5100014 . I can't say more, because I don't see your project structure. For me it works from sbt Scala console. – zhelezoglo Jan 23 '17 at 23:19
  • Thank you!! yes, that was the error, thank you so much, have a good day! – Jquiro12 Jan 24 '17 at 00:11