Scala? What is Scala? Scala ... – combines object-oriented and functional paradigms – is statically (and strongly) typed, with local type-inference – runs on the JVM and has the same, class-based model – is interoperable with Java/other languages on the JVM – is a relatively small, orthogonal language (Scheme < Scala < Java < C# < C++) – includes a powerful standard library
OO Lessons from OO “Objects are charatcerized by state, identity, and behavior” — (Booch) Scala – Eliminate/reduce mutable state – Prefer structural equality to reference identity – Focus on behavior/functionality
FP Lessons from FP Functional, or “expression-oriented” programming allows better reasoning about code because of focus on ... – Referential transparency – Equational reasoning – Immutability – “What” not “How”
Scala's approach • OO/FP is orthogonal – both tools have proven themselves – we don't want to give up one for the other • Combine – light-weight, “agile” syntax – safety and performance of static typing
Java final int truth = 42; String pet = “turtle”; long sum(long a, long b) { return a + b; } Scala val truth = 42 var pet = “turtle” def sum(a: Long, b: Long) = a + b lazy val expensive = compute()
Objects Classes & Objects Java public class Foo { public Foo(String name) { this.name = name } final String name; public static int bar = 42; private static void bla() { System.out.println(“Bla!”) } } Scala class Foo(val name: String) object Foo { var bar = 42 private def bla = println(“Bla!”) }
Objects Classes & Objects Java public class Foo { public Foo(String name) { this.name = name } final String name; public static int bar = 42; private static void bla() { System.out.println(“Bla!”) } } Scala class Foo(val name: String) object Foo { var bar = 42 private def bla = println(“Bla!”) }
Type macros type PostgreSQLDriver = macro ... object DB extends PostgreSQLDriver(“path/to/db”) for { coffee DB.Coffees ← if coffee.price > 9.95 } yield (coffee.name, coffee.sales)
Main documentation site docs.scala-lang.org Style guide docs.scala-lang.org/style API documentation (ScalaDoc) scala-lang.org/api/nightly Language specification scala-lang.org/docu/files/ScalaReference.pdf
Books Scala for the Impatient (Cay Horstmann) typesafe.com/resources/scala-for-the-impatient Programming in Scala (Martin Odersky) artima.com/shop/programming_in_scala_2ed Scala in Depth (Josh Suereth) manning.com/suereth Functional Programming in Scala (Chiusano/Bjarnason) manning.com/bjarnason Introduction to the Art of Programming Using Scala programmingusingscala.net (Mark Lewis)
Web Play! playframework.org Ruby-on-Rails style, convention-over-configuration web framework for Java and Scala leverages static typing to report errors at compile-time while still allowing F5-like development Concurrency Akka akka.io Toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications
• Feature imports … for experimental/dangerous language features … “I understand that X might change in the future” import language.experimental.macros • Deprecation elements need to be @deprecated for at least one major version before removal • Compatibility – Different major releases cannot be combined – Latest Scala version with all its features can be deployed on Java versions as old as Java 5
Why Scala • Consistency and safety • Performance – Uses the best GC on this planet – World-class JIT-compiler • Broad tooling support • Leverage all your existing libraries of the JVM eco-system FUN!
The same, rich API for all sequences Accessing the i-th element ... val arr = Array(1, 2, 3) arr(i) val lst = List(1, 2, 3) lst(i) val buf = Buffer(1, 2, 3) buf(i) val vec = Vector(1, 2, 3) vec(i)
The same, rich API for all sequences Length of the collection val arr = Array(1, 2, 3) arr.length val lst = List(1, 2, 3) lst.length val buf = Buffer(1, 2, 3) buf.length val vec = Vector(1, 2, 3) vec.length