Reactive Systems Consultant at manuel.bernhardt.io
Dec. 3, 2013•0 likes•3,005 views
1 of 61
Scala - Java2Days Sofia
Dec. 3, 2013•0 likes•3,005 views
Download to read offline
Report
Technology
This is a short introduction to the Scala programming language and its ecosystem, giving some background about how the language came to be and a quick taste of what it looks like. It also shortly introduces a few tools and technologies related to it
8. SCALA DESIGN GOALS
• Full
interoperability with Java
@elmanu
Dienstag, 03. Dezember 13
9. SCALA DESIGN GOALS
• Full
interoperability with Java
• Cut
down boilerplate
@elmanu
Dienstag, 03. Dezember 13
10. SCALA DESIGN GOALS
• Full
interoperability with Java
• Cut
down boilerplate
• Pure
@elmanu
Dienstag, 03. Dezember 13
object orientation & functional programming
11. SCALA DESIGN GOALS
• Full
interoperability with Java
• Cut
down boilerplate
• Pure
object orientation & functional programming
• Move
@elmanu
Dienstag, 03. Dezember 13
away from null
12. SCALA DESIGN GOALS
• Full
interoperability with Java
• Cut
down boilerplate
• Pure
object orientation & functional programming
• Move
away from null
• Multi-core
@elmanu
Dienstag, 03. Dezember 13
programming
13. AGENDA
• History
• Why
Scala?
• Scala
in the wild
• The
Code / Scala in
practice
• Tools
@elmanu
Dienstag, 03. Dezember 13
& more
14. WHAT PEOPLE SAY
If I were to pick a language today
other than Java, it would be Scala.
James Gosling
Father of Java
@elmanu
Dienstag, 03. Dezember 13
15. 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
http://macstrac.blogspot.co.at/2009/04/scala-as-long-term-replacement-for.html
@elmanu
Dienstag, 03. Dezember 13
18. SCALA IN THE WILD
etc.
@elmanu
Dienstag, 03. Dezember 13
19. SCALA IN THE WILD
@elmanu
Dienstag, 03. Dezember 13
20. SCALA IN THE WORLD
http://www.scala-tribes.org/
@elmanu
Dienstag, 03. Dezember 13
21. SCALA IN VIENNA
• Scala Vienna
•1
User Group
year old, 124+ members
• http://scala-vienna.org/
@elmanu
Dienstag, 03. Dezember 13
22. AGENDA
• History
• Why
Scala?
• Scala
in the wild
• The
Code / Scala in
practice
• Tools
@elmanu
Dienstag, 03. Dezember 13
& more
23. AVOIDING THE BILLIONDOLLAR 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
@elmanu
Dienstag, 03. Dezember 13
24. AVOIDING THE BILLIONDOLLAR 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")
@elmanu
Dienstag, 03. Dezember 13
27. 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() {
!
!
!
}
!
@Override public boolean equals(Object that) {
!
!
!
}
}
@elmanu
Dienstag, 03. Dezember 13
// yada yada yada
// yada yada yada
28. 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)
@elmanu
Dienstag, 03. Dezember 13
40. 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()
}
}
}
@elmanu
Dienstag, 03. Dezember 13
41. 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()
}
}
}
@elmanu
Dienstag, 03. Dezember 13
42. MACROS
• Compile-time, during
• Expanding
type checking
the AST
• Experimental
since Scala 2.10
http://scalamacros.org
@elmanu
Dienstag, 03. Dezember 13
44. 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))
@elmanu
Dienstag, 03. Dezember 13
45. MACROS
PLAY JSON DE/SERIALIZATION
import play.api.json._
implicit val creatureFormat = Json.format[Creature] // format is a macro
@elmanu
Dienstag, 03. Dezember 13
46. AGENDA
• History
• Why
Scala?
• Scala
in the wild
• The
Code / Scala in
practice
• Tools
@elmanu
Dienstag, 03. Dezember 13
& more
48. SIMPLE BUILD TOOL
name := "My Project"
version := "1.0"
organization := "org.myproject"
libraryDependencies += "org.scala-tools.testing" %% "scalacheck" %
"1.8" % "test"
libraryDependencies ++= Seq(
"net.databinder" %% "dispatch-meetup" % "0.7.8",
"net.databinder" %% "dispatch-twitter" % "0.7.8"
)
javaOptions += "-Xmx256m"
logLevel in compile := Level.Warn
@elmanu
Dienstag, 03. Dezember 13
49. PLAY! FRAMEWORK
• MVC
web framework,
inspired by RoR and Django
• Java, Scala
• Everything
is compiled
& scalable
performance
• Predictable
• Fun
to work with!
@elmanu
Dienstag, 03. Dezember 13
50. PLAY! FRAMEWORK
• MVC
web framework,
inspired by RoR and Django
• Java, Scala
• Everything
is compiled
& scalable
performance
• Predictable
• Fun
to work with!
@elmanu
Dienstag, 03. Dezember 13
51. AKKA
• Toolkit
for building
concurrent & distributed
applications more easily
• Actors
as concurrency
model, simpler to reason
with than threads
• Remoting
and
clustering support
@elmanu
Dienstag, 03. Dezember 13
54. FRAMEWORKS: AKKA
• Actor
concurrency model based on Erlang
• “Human” design: actors
hierarchies
• Much, much, much
threads
@elmanu
Dienstag, 03. Dezember 13
talk to eachother and form
simpler to work and reason with than
57. FRAMEWORKS: AKKA
class Worker extends Actor {
! def receive = {
!
!
!
!
}
@elmanu
Dienstag, 03. Dezember 13
! case Process(doc: Document) =>
! ! val processed = doSomeHardWork(doc)
! ! sender ! Result(processed)
}
58. FRAMEWORKS: PLAY
• MVC
framework à la Rails
• Everything
•I
is compiled
mean everything: CSS, JavaScripts, Templates, URLs, JSON, ...
@elmanu
Dienstag, 03. Dezember 13