O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required