Why Scala Native?

Scala Native is an ahead-of-time machine code compiler for the Scala programming language. It can take a Scala program, with traits, objects, garbage collection, and other advanced features of Scala, and translate it down to the same kind of executable machine code that a C compiler would output.

But that’s not all. On top of Scala’s support for object-oriented and functional programming, Scala Native adds powerful capabilities for working much closer to bare metal. In particular, it provides access to OS-level I/O and networking APIs, system-level shared libraries, and C-style memory management. With these techniques, we can often replace C code in performance-critical applications. And Scala’s capacity for clean abstraction means that we can make low-level programs more elegant and readable than ever before.

At its best, Scala Native can simultaneously exhibit both modern programming techniques and a close affinity for the underlying hardware. This expressive clarity also makes Scala Native a great way to learn systems programming for the first time.

To start, let’s set up a Scala Native project. We’ll do so much as we would set up a simple Scala project: by creating a new folder (let’s call ours my_code/hello/) with three files. The first file is a build.sbt file that describes our project:

InputAndOutput/hello/build.sbt
 name := "hello"
 enablePlugins(ScalaNativePlugin)
 
 scalaVersion := "2.11.8"
 scalacOptions ++= Seq("-feature")
 nativeMode := "debug"
 nativeGC := "immix"

The second is a hello.scala file that contains our code:

InputAndOutput/hello/hello.scala
 object​ main {
 def​ main(args​:​​Array​[​String​]) {
  println(​"hello, world!"​)
  }
 }

And the third is a project/plugins.sbt file that imports the actual Scala Native plugin:

InputAndOutput/hello/project/plugins.sbt
 addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.0-M2")

Much like a regular Scala program, when you enter the command, sbt run, the Scala build tool (sbt)[15] builds the project for you. After it’s fully compiled, you should see the expected output of the program, like this:

 $ ​​sbt​​ ​​run
 [warn] Executing in batch mode.
 [warn] For better performance, hit [ENTER] to switch to interactive mode,
 [warn] or consider launching sbt without any commands, or explicitly
 [warn] passing 'shell'
 [info] Loading project definition from /root/project-build/project
 [info] Set current project to sn-mem-hacks
 [info] (in build file:/root/project-build/)
 [info] Compiling 1 Scala source to
 [info] /root/project-build/target/scala-2.11/classes...
 [info] 'compiler-interface' not yet compiled for Scala 2.11.8. Compiling...
 [info] Compilation completed in 13.051 s
 [info] Linking (2352 ms)
 [info] Discovered 1267 classes and 9344 methods
 [info] Optimizing (5002 ms)
 [info] Generating intermediate code (1015 ms)
 [info] Produced 39 files
 [info] Compiling to native code (2272 ms)
 [info] Linking native code (153 ms)
 hello, world
 [success] Total time: 28 s, completed Mar 13, 2018 5:11:03 PM

This is exactly what we would expect from a regular Scala program: the code we used is identical to a Hello, World in standard Scala, and the build configuration has only added a single plugin to support Scala Native. This is good news. Scala Native is 100% Scala—it’s not a variant or a new version. It’s simply a plugin that gives the language some additional capabilities.