Question

I am reading Martin Odersky's Programming in Scala, and I have been using vi and the command line to compile so far. I want to learn to use Eclipse and the Scala-IDE plug-in, but I lack a basic understanding of compiling and executing multiple source code files in Eclipse.

My IDE is as follows:

  • Ubuntu 12.04
  • Eclipse 3.7.2
  • Scala Plugin 2.0.1
  • Scala Library 2.9.2

I am using the Checksum example in Chapter 4 for practice. The source code is as follows:

Listings 4.1 & 4.2 / ChecksumAccumulator.scala:

class ChecksumAccumulator {
    private var sum = 0
    def add(b: Byte) { sum += b }
    def checksum(): Int = ~(sum & 0xFF) + 1
}

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
        }
}  

Listing 4.3 / Summer.scala:

import ChecksumAccumulator.calculate

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

I can compile these classes using scalac Summer.scala ChecksumAccumulator.scala at the command line. I can then execute the object code with the command line scala Summer of love, which returns the checksum results for "of" and "love" (-213 and -182, respectively).

How would I build the object code using Eclipse and not the command line, and how would I call the Summer object code through Eclipse?

Was it helpful?

Solution

In Eclipse, you can just create a new Scala project, and put the source files there. Once the files are part of the same project, there's no need to do anything special:

Just click on Run >> Run As >> Scala Application (or press Alt + Shift + X, S)

Have fun learning Scala :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top