Introduction to the Scala programming language @ the Java Klassentreffen 2013 in Vienna and Linz organized by http://www.javaklassentreffen.at
Introducing the background, design goals, and some examples (from a Java perspective)
http://java.at/web/guest/jkt13
9. @elmanu Java Klassentreffen 2013 - javatraining.at
SCALA DESIGN GOALS
• Full interoperability with Java
• Cut down boilerplate
10. @elmanu Java Klassentreffen 2013 - javatraining.at
SCALA DESIGN GOALS
• Full interoperability with Java
• Cut down boilerplate
• Pure object orientation & functional programming
11. @elmanu Java Klassentreffen 2013 - javatraining.at
SCALA DESIGN GOALS
• Full interoperability with Java
• Cut down boilerplate
• Pure object orientation & functional programming
• Move away from null
12. @elmanu Java Klassentreffen 2013 - javatraining.at
SCALA DESIGN GOALS
• Full interoperability with Java
• Cut down boilerplate
• Pure object orientation & functional programming
• Move away from null
• Multi-core programming
13. @elmanu Java Klassentreffen 2013 - javatraining.at
AGENDA
• History
• Why Scala?
• Scala in the wild
• The Code / Scala in practice
• Tools & more
14. @elmanu Java Klassentreffen 2013 - javatraining.at
WHAT PEOPLE SAY
If I were to pick a language today
other than Java, it would be Scala.
James Gosling
Father of Java
15. @elmanu Java Klassentreffen 2013 - javatraining.at
WHAT PEOPLE SAY
I can honestly say if someone had
shown me the Programming Scala
book by Martin Odersky, Lex Spoon
& Bill Venners back in 2003 I’d
probably have never created Groovy.
James Strachan
Creator of Groovy
20. @elmanu Java Klassentreffen 2013 - javatraining.at
SCALA INVIENNA
• ScalaVienna User Group
• http://scala-vienna.org/
Next meeting: 26th
September 6 PM - Sektor 5
21. @elmanu Java Klassentreffen 2013 - javatraining.at
AGENDA
• History
• Why Scala?
• Scala in the wild
• The Code / Scala in
practice
• Tools & more
22. @elmanu Java Klassentreffen 2013 - javatraining.at
AVOIDINGTHE BILLION-
DOLLAR MISTAKE
But I couldn't resist the
temptation to put in a null
reference, simply because it
was so easy to implement
Tony Hoare
Creator of ALGOL
23. @elmanu Java Klassentreffen 2013 - javatraining.at
AVOIDINGTHE BILLION-
DOLLAR MISTAKE
val maybeUser: Option[User] = User.findOneByName("bob")
// returns Some[User]
maybeUser == None // false
maybeUser.foreach { user =>
println(user.fullName)
// prints "Bob Marley" if there is a user!
}
val name = maybeUser.map(_.name).getOrElse("Unknown user")
25. @elmanu Java Klassentreffen 2013 - javatraining.at
CONCISENESS
class User(
var name: String,
var surname: String,
var email: String)
val bob = new User("Bob", "Marley", "bob@marley.org")
// bob: User = User@5c3f1224
bob.name // res0: String = Bob
bob.name = "Bobby" // bob.name: String = Bobby
26. @elmanu Java Klassentreffen 2013 - javatraining.at
CONCISENESS
public class ImmutableUser {
! private final String name;
! private final String surname;
! private final String email;
! public ImmutableUser(String name, String surname, String email) {
! ! this.name = name;
! ! this.surname = surname;
! ! this.email = email;
! }
! public String getName() {
! ! return this.name;
! }
! public String getSurname() {
! ! return this.surname;
! }
! public String getEmail() {
! ! return this.surname;
! }
! @Override public int hashCode() {
! ! // yada yada yada
! }
! @Override public boolean equals(Object that) {
! ! // yada yada yada
! }
}
27. @elmanu Java Klassentreffen 2013 - javatraining.at
CONCISENESS
case class ImmutableUser(
name: String,
surname: String,
email: String)
val bob = ImmutableUser("Bob", "Marley", "bob@marley.org")
// hashcode and equals for free!
val namedBob = ImmutableUser(name = "Bob", surname = "Marley",
email = "email")
val bobby = bob.copy(name = "Bobby")
// returns a User with name Bobby
bob.toString // res0: String = ImmutableUser(Bob,Marley,email)
28. @elmanu Java Klassentreffen 2013 - javatraining.at
USEFULTYPE INFERENCE
val foo = "Bar" // foo: String = Bar
val answer = 42 // answer: Int = 42
val price = 9.99 // price: Double = 9.99
val nums = List(1, 2, 3) // nums: List[Int] = List(1, 2, 3)
val map = Map("abc" -> List(1, 2, 3))
// map: scala.collection.immutable.Map[String,List[Int]] =
Map(abc -> List(1, 2, 3))
29. @elmanu Java Klassentreffen 2013 - javatraining.at
EXPLICITTYPING
val foo: String = "Bar" // foo: String = Bar
val answer: Int = 42 // answer: Int = 42
val price: Double = 9.99 // price: Double = 9.99
val nums: List[Int] = List(1, 2, 3) // nums: List[Int] =
List(1, 2, 3)
val map: Map[String, List[Int]] = Map("abc" -> List(1, 2, 3))
// map: scala.collection.immutable.Map[String,List[Int]] =
Map(abc -> List(1, 2, 3))
37. @elmanu Java Klassentreffen 2013 - javatraining.at
• Minimal language, powerful library
• Language features for extensibility
EXTENSIBLE LANGUAGE
38. @elmanu Java Klassentreffen 2013 - javatraining.at
DOMAIN SPECIFIC
LANGUAGES
import collection.mutable.Stack
import org.scalatest._
class ExampleSpec extends FlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
a [NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
}
40. @elmanu Java Klassentreffen 2013 - javatraining.at
MACROS
PLAY JSON DE/SERIALIZATION
case class Creature(name: String, isDead: Boolean, weight: Float)
implicit val creatureReads: Reads[Creature] = (
(__ "name").read[String] and
(__ "isDead").read[Boolean] and
(__ "weight").read[Float]
)(Creature)
implicit val creatureWrites: Writes[Creature] = (
(__ "name").write[String] and
(__ "isDead").write[Boolean] and
(__ "weight").write[Float]
)(unlift(Creature.unapply))
41. @elmanu Java Klassentreffen 2013 - javatraining.at
MACROS
PLAY JSON DE/SERIALIZATION
import play.api.json._
implicit val creatureFormat = Json.format[Creature] // format is a macro
42. @elmanu Java Klassentreffen 2013 - javatraining.at
AGENDA
• History
• Why Scala?
• Scala in the wild
• The Code / Scala in practice
• Tools & more
45. @elmanu Java Klassentreffen 2013 - javatraining.at
FRAMEWORKS:AKKA
• Actor concurrency model based on Erlang
• “Human” design: actors talk to eachother and form hierarchies
• Much, much, much simpler to work and reason with than
threads