0
votes

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?

1
I am not familiar with Scala. But according to my understating , You have to create a Scala Project in eclipse include your Source file in source directory. After that Right click on source file then select run . scala-ide.org/docs/tutorials/lift24scalaide20/index.htmlnayakam
Create a scala project and switch to the scala perspective,put both file in some package of the project.Run scala with Ctrl+F11 and choose to run as scala application.BTW,if you're not familiar with java I suggest you should not learn scala at first.Hongxu Chen
I'd highly recommend to watch the following screencasts: Getting Started with the Scala IDE and Scala IDE Features Overview. In about 15 minutes you will be a Scala IDE pro ;-)Mirco Dotta
Thank you all very much. I plan on going through each of these methods after work today. Hongxu, I am familiar with Java, but I only know how to build and deploy .war files with Maven in Eclipse. In school, I used JGrasp, which automatically associated all open source code files into the same compilation. Once I finish the book, I plan on using Maven and the Lift framework, but I want to learn the fundamentals first.David Kaczynski

1 Answers

1
votes

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 :)