Compiling Scala

You want to compile the code explicitly if you have multiple files or want to distribute the bytecode instead of the source code. Here’s how to write a piece of Scala code and compile it using the scalac compiler. In the following example, we define a small executable code in an object named Sample that extends the App trait—you’ll soon learn about Scala singleton objects and traits. App instructs the compiler to generate the necessary main method to make Sample the starting class.

FirstStep/Sample.scala
 
object​ Sample ​extends​ App {
 
println(​"Hello Scala"​)
 
}

Compile the code with the command scalac Sample.scala, and then run it using either the scala tool or the java command. To use the scala tool, type scala Sample

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.