Advertisement

Online Scala Compiler

Write, run and test Scala code online, right in your browser. This free online Scala compiler needs no installation or signup — just type your code and click Run.

Scala blends object-oriented and functional programming on the JVM and is widely used for data engineering with Apache Spark.

How to use the Scala compiler

  1. Write or paste your Scala code in the editor.
  2. If your program reads input, open the I/O tab and type it into the STDIN box (one value per line).
  3. Click Run — the output and any errors appear in the output panel.

Sample: read input in Scala

import scala.io.StdIn

object Main extends App {
  val name = StdIn.readLine()
  println(s"Hello, $name!")
}

Paste this into the editor, type your name in the STDIN box and click Run.

More Scala examples to try

Case classes and pattern matching

Pattern matching is one of Scala's most powerful features.

sealed trait Shape
case class Circle(r: Double) extends Shape
case class Rect(w: Double, h: Double) extends Shape

object Main extends App {
  def area(s: Shape): Double = s match {
    case Circle(r)  => math.Pi * r * r
    case Rect(w, h) => w * h
  }
  List(Circle(1), Rect(2, 3)).foreach(s => println(f"$s%-15s ${area(s)}%.2f"))
}

Functional collections

Transform data with map, filter and foldLeft.

object Main extends App {
  val nums = (1 to 10).toList
  val evensSquared = nums.filter(_ % 2 == 0).map(n => n * n)
  val total = evensSquared.foldLeft(0)(_ + _)
  println(s"Even squares: $evensSquared")
  println(s"Total: $total")
}

Common Scala errors and how to fix them

error: type mismatch; found: String, required: Int
Convert explicitly, e.g. "42".toInt.
error: reassignment to val
val is immutable. Use var, or better, create a new value.
scala.MatchError
No case matched the value. Add the missing case, or a default case _ =>.
error: not found: value x
The name is misspelled, out of scope, or missing an import.

Frequently asked questions

Is this online Scala compiler free?

Yes. It is completely free to use, with no signup required to write and run code.

Do I need to install anything?

No. It all works from your web browser, so there is nothing to download or set up — it works on Windows, macOS, Linux, Chromebooks and phones.

How do I give input to my Scala program?

Before clicking Run, type the input into the STDIN box under the I/O tab, one value per line. Your program reads it as standard input, just like typing in a terminal.

Is this good for learning Scala?

Yes. It is ideal for practising exercises, testing small programs and trying out ideas from tutorials, because you can run code instantly without configuring an environment.

Other online compilers

Editor powered by OneCompiler.