Slide 1

Slide 1 text

Introduction to Scala Introduction to Scala

Slide 2

Slide 2 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 2 2 What is 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

Slide 3

Slide 3 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 3 3 Lessons from 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

Slide 4

Slide 4 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 4 4 Lessons from 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”

Slide 5

Slide 5 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 5 5 Scala's approach 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

Slide 6

Slide 6 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 6 6 Syntax Syntax 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()

Slide 7

Slide 7 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 7 7 Methods Methods Java BigInteger cats = BigInteger.valueOf(5); BigInteger dogs = BigInteger.valueOf(12); BigInteger pets = cats.add(dogs) Scala val cats: BigInt = 5 val dogs: BigInt = 12 val pets = cats + dogs ● Names are freely selectable ● Binary methods can leave out dot and parentheses object.method(argument) ↔ object method argument

Slide 8

Slide 8 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 8 8 Classes & 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!”) }

Slide 9

Slide 9 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 9 9 Classes & 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!”) }

Slide 10

Slide 10 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 10 10 Comparison to Java Comparison to Java Scala removes... ● Unnecessary ; {} return ● Static fields, methods, classes, ... ● Operators (“+”, “-”, “*”, ...) ● Primitive types (int, long, ...) ● Special syntax for arrays ([]), ... ● Raw types ● Checked exceptions ● break/continue ● … and a lot more Scala adds... ● Better type system ● Type inference ● Traits ● Lambdas/closures/functions ● First-class modules ● Pattern matching ● Laziness and call-by-name ● XML literals ● Named and default arguments

Slide 11

Slide 11 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 11 11 Demo

Slide 12

Slide 12 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 12 12 New in Scala 2.10 New in Scala 2.10 ● Reflection library ● Experimental macros ● Implicit classes, value classes, string interpolation, … ● scala.Dynamic class ● Collection performance improvements and additions ● Reduced installation footprint ● Faster and better optimizer ● New backends for Java 5, Java 6 and Java 7

Slide 13

Slide 13 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 13 13 Planned for 2.11 Planned for 2.11 ● Compile-time and run-time performance ● Refinement of the reflection API ● Extension of the macro mechanism

Slide 14

Slide 14 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 14 14 Type macros 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)

Slide 15

Slide 15 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 15 15 Type macros Type macros Other potential use cases? object PayrollXML extends XMLSchema(“payroll.xsd”) object WeatherWebService extends WSDL(“example.com/weather.wsdl”) object LibXML extends ELF64Provider(“libxml2.so”)

Slide 16

Slide 16 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 16 16 Tooling Tooling IDEs and Editors Eclipse scala-ide.org IntelliJ confluence.jetbrains.net/display/SCA/ Getting+Started+with+IntelliJ+IDEA+Scala+Plugin REPL bundled with Scala Netbeans, Emacs, Vim, supported …

Slide 17

Slide 17 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 17 17 Tooling Tooling Build tools SBT scala-sbt.org Maven maven-scala-plugin sonatype.com/books/mcookbook/reference/scala.html Buildr, Ant, etc. also supported

Slide 18

Slide 18 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 18 18 Tooling Tooling ● Debugging, instrumentation, profiling, etc. Keep using all the existing tools for the JVM! – VisualVM – Jrebel – AspectJ – …

Slide 19

Slide 19 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 19 19 Documentation Documentation 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

Slide 20

Slide 20 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 20 20 Courses and articles Courses and articles Coursera: Functional Programming Principles in Scala coursera.org/course/progfun Scala for Java Refugees codecommit.com/blog/scala/roundup-scala-for-java-refugees

Slide 21

Slide 21 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 21 21 Books Books 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)

Slide 22

Slide 22 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 22 22 Libraries Libraries Database abstraction Slick slick.typesafe.com Modern database query and access library Scalaz Scalaz github.com/scalaz/scalaz

Slide 23

Slide 23 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 23 23 Frameworks Frameworks 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

Slide 24

Slide 24 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 24 24 Libraries/Frameworks Libraries/Frameworks Testing ScalaTest playframework.org ScalaCheck github.com/rickynils/scalacheck Specs etorreborre.github.com/specs2

Slide 25

Slide 25 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 25 25 Versioning Versioning 2.10.0-0 Epoch . Major . Minor-Update

Slide 26

Slide 26 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 26 26 Policies Policies ● 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

Slide 27

Slide 27 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 27 27 Migration Manager Migration Manager

Slide 28

Slide 28 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 28 28 Why Scala 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!

Slide 29

Slide 29 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 29 29 Users Users

Slide 30

Slide 30 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 30 30 Thanks! Questions?

Slide 31

Slide 31 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 31 31 History History 1996 Java 1.0 (James Gosling) 1997 Pizza (Martin Odersky & Philip Wadler) – Superset of Java – Has generics, case classes and higher-oder functions – Compiles to Java source code 1998 GJ (Bracha, Odersky, Stoutamire and Wadler) – Carries over generics from Pizza – Übersetzt zu Java-Bytecode 2000 Java 1.3 (GJ becomes javac, generics disabled) 2004 Java 1.5 (Generics finally enabled)

Slide 32

Slide 32 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 32 32 Type hierarchy Type hierarchy

Slide 33

Slide 33 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 33 33 Collections Collections The same, rich API for all sequences … ● Choose freely, based on your algorithm ● Change is cheap ● Defaults are sensible

Slide 34

Slide 34 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 34 34 Collections Collections The same, rich API for all sequences Creation ... val arr = Array(1, 2, 3) val lst = List(1, 2, 3) val buf = Buffer(1, 2, 3) val vec = Vector(1, 2, 3)

Slide 35

Slide 35 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 35 35 Collections Collections 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)

Slide 36

Slide 36 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 36 36 Collections Collections 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

Slide 37

Slide 37 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 37 37 Pattern matching Pattern matching

Slide 38

Slide 38 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 38 38 Comprehensions Comprehensions

Slide 39

Slide 39 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 39 39 Implicits Implicits

Slide 40

Slide 40 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 40 40 Typeclasses Typeclasses

Slide 41

Slide 41 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 41 41 Reflection Reflection

Slide 42

Slide 42 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 42 42 Dark corners Dark corners ● Null ● Lossy numeric conversions ● No (side) effect checking ● scala-lang.org

Slide 43

Slide 43 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 43 43 Platforms Platforms ● JVM, Java 5 or later – OpenJDK, Oracle JDK – Avian, JamVM, JamaicaVM* ● Android ● Less interesting: – CLR – LLVM – JavaScript via GWT

Slide 44

Slide 44 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 44 44 New in Scala 2.8 New in Scala 2.8 ● New sequential collections ● scala.swing package ● Command completion in REPL ● Named and default method names ● implicit conversions ● Type specialization ● Package objects, Better Annotations, …

Slide 45

Slide 45 text

01/16/2013 01/16/2013 Simon Ochsenreither Simon Ochsenreither 45 45 New in Scala 2.9 New in Scala 2.9 ● New parallel collections ● scala.sys package ● Faster and better REPL ● Binary compatiblity guarantees ● Better try-catch-finally