Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Scala

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for soc soc
January 15, 2013

Introduction to Scala

Avatar for soc

soc

January 15, 2013
Tweet

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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”
  4. 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
  5. 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()
  6. 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
  7. 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!”) }
  8. 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!”) }
  9. 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
  10. 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
  11. 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
  12. 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)
  13. 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”)
  14. 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 …
  15. 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
  16. 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 – …
  17. 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
  18. 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
  19. 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)
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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!
  25. 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)
  26. 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
  27. 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)
  28. 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)
  29. 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
  30. 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
  31. 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
  32. 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, …
  33. 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