Why Scala? Simply The Only Language has Enough Expressiveness

  • rich type system, especially Unit, Nothing

    Exception has type Nothing. can be returned instead of value of any type

  • pattern matching on almost anything

List(1,2,3) match {
  case Nil        => println("The list is empty")
  case x @ h :: _ => println(s"$x starts from: $h")
}
// List(1, 2, 3) starts from: 1
  • currying
  • partial functions
  • infix notation
  • postfix operator notation
  • tail recursion
  • custom unapply
  • named parameters
  • default parameter values
  • case classes. Instead of
class Point {
  int x;
  int y;

  Point(int x, int y) {
        this.x = x;
        this.y = y;
  }
  // equals, hashcode, toString...
}
Point p = new Point(10, 20);

we simply do

case class Point(x: Int, y: Int)
val p = Point(10, 20)
// p: Point = Point(x = 10, y = 20)

equals, hashcode, toString, unapply, copy out of the box provided by the compiler

  • tuples
  • objects
  • implicits/extensions methods
  • generics/high-order generics
  • immutable collections
  • can be compiled to JVM/JS targets
  • sealed traits
  • high-order functions
  • macros (compile-time reflection) / derivations
  • amazing build tools + sbt plugins, configuration as a code
  • rich basic library
  • JVM interop
  • unchecked exceptions
  • compiler optimizations: final val, @inline
  • compiler matching exhaustiveness check
sealed trait Color
case object Red extends Color
case object Yellow extends Color
case object Green extends Color

def show(c: Color): String = c match {
  case Red => "Red"
  case Green => "Green"
}
// error: match may not be exhaustive.
// It would fail on the following input: Yellow
// Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=other-match-analysis, site=repl.MdocSession.MdocApp.show
// def show(c: Color): String = c match {
//                              ^
  • underscore syntax for different semantics: all, nothing, once
  • import in any place
  • import from the object scope
  • expression block in any place {...}

will add more once recall