To run a Scala program, you must supply the name of a standalone signleton object with a main method that takes on parameter, an Array[String], and has a result type of Unit.
import ChecksumAccumulator.calculate
object Summer{
def main(args: Array[String]){
for(arg <- args="" p=""> println(arg + ": " + calculate(arg))
}
}
Scala implicitly imports members of packages java.lang and scala, as well as the members of a singlecton object named Predef, into every Scala source file. Predef, which resides in package scala, contains many useful methods. (println,assert...)
.scala files anything you want, no matter what Scala classes or code you put in them.
In general in the case of non=scripts, however, it is recommended style to name files after the classes they contain as in done in java, so that programmers can more easily locate classes by looking at file names.
A script, by contrast, must end in a result expression.
One way to do this is to use scalac, which is the basic Scala compiler, like this:
$scalac ChecksumAccumulator.scala summer.scala
This compiles your source files, but there may be a perceptible delay before the compilation finishes. The reason is that every time the compiler starts up, it spends time scanning the contents of jar files and doing other initial work before it even looks at the fresh source files you submit to it. Fro this reason, the Scala distribution also includes a Scala compiler daemon called fsc(for fast Scala compiler). You use it like this:
$fac ChecksumAccumulator.scala Summer.scala
If you ever want to stop the fsc daemon, you can do so with fsc -shutdown.
->
댓글 없음:
댓글 쓰기