4. What is Scala?
General purpose, compiled language
Relatively new (2003)
Designed to express common programming patterns
Concisely
Elegantly
In a type-safe way
Originally intended to run on both the JVM and the CLR
After some time, the .NET implementation fell aside
Later on, thanks to a grant from Microsoft, work resumed
Resembles dynamic languages such as Python / Ruby
Combines OO and functional languages features
15-May-11 WWW.TIKALK.COM
5. Personal Issues
Created by Martin Odersky, a computer scientist at EPFL
in Lausanne, Switzerland.
Co-designer of Java's generics, and the original author of
the current javac
James Gosling:
"If I needed to choose a language other than Java, it would
be Scala"
James Strachan (Groovy creator) has left Groovy and
now advocates Scala
"If I had known Scala when I created Groovy, I would have
probably not created it at all "
15-May-11 WWW.TIKALK.COM
6. Static Typing
Scala is a statically-typed language, unlike Ruby or Python
Better IDE assistance
Code completion
Refactoring support
Developer productivity enhanced
Safer code via compile-time checks
Better performance
Enhancing developer productivity
No need to wait for JSR-292's invokedynamic for performance
boost
As in Groovy, JRuby, Jython, …
Code size is typically reduced by a factor of 2-3 compared to
Java
15-May-11 WWW.TIKALK.COM
7. Compatibility
The Scala compiler generates Java bytecode
100% compatible with the JRE
Seamless use Java objects in Scala and vice versa
Deploy Scala applications to any JEE container / Android
Reuse existing tools, servers, libraries
Keep existing knowledge & workers
Make the change gradually
Java -> Jala -> Scala
15-May-11 WWW.TIKALK.COM
8. What Went Wrong with Java?
Oh, quite a few things:
Java is over-verbose
Stopped evolving
Long adoption of new development idioms
Functional programming, for instance
Political / Marketing Issues (ok, Oracle)
"Learning Java stunts your intellectual growth as a
developer" [at least, some would say]
15-May-11 WWW.TIKALK.COM
9. Why Prefer Scala Over Java ?
Scala corrects many of Java's mistakes:
Everything is an Object
Type inference
var dummy= Map[String,Int]()
True Mixins, Traits, Duck Typing
Functional programming language concepts
Fresh start, no backward compatibility constraints
Generics, anyone?
More compact
No getters, setters, equals(), hashCode(), toString(),...
15-May-11 WWW.TIKALK.COM
10. ...Scala over Java
Allows using new idioms and ways of programming
Mostly just too laborious to do in Java
Less LOC doing the same amount of work
Better readability
Never use anonymous inner classes again
But still, leverage your Java code from Scala
Productivity benefits
Looks like a dynamically typed language
While actually being 100% statically typed
15-May-11 WWW.TIKALK.COM
11. Polyg-wha?
It is common advice that you should learn multiple
programming languages
Scala integrates functional programming with OO
programming
Scala allows you to gradually learn functional techniques
while still being able to use familiar object-oriented
techniques
Useful when migrating from Java
Learning Scala may be easier than learning non-
OO functional languages
15-May-11 WWW.TIKALK.COM
12. Functional Concepts in Scala
Functions are first-class values
Convenient closure syntax
Lazy evaluation
Pattern matching
Tailcall optimization
Powerful generics
(*) Still, Scala feels less functional because its core syntax is largely in the
tradition of Java, not Lisp, ML, or Haskell, the three most prominent
ancestors of functional language families)
15-May-11 WWW.TIKALK.COM
13. Performance
On par with Java
Some features of Scala can produce faster code
Specialization
Inlining
Laziness
...While other features makes it slower
15-May-11 WWW.TIKALK.COM
15. Higher Order Functions
Functions are first-class objects
Can be assigned to variables
Passed to other functions
Same as any other data type
Code made more concise and readable
...not quite as concise as in some other functional
languages
But much, much better than the equivalent Java code
15-May-11 WWW.TIKALK.COM
16. Side Effects Problem
In OO languages, objects may contain state - mutable
instance data
When a method uses mutable data, it now has side effects
Reading or writing any state which is not an incoming or
outgoing argument
Functions with side effects
Are harder to test
Harder to reason about
In general harder to get right
As more side effects are added, they accumulate, making
the composed function even more difficult to get right
15-May-11 WWW.TIKALK.COM
17. Immutable Values
Using immutable values...
Makes it easier to write code without side effects
Reduces the likelihood of concurrency bugs
Can make code easier to read and understand
Scala separates the concept of a val from a var:
A val in Scala is like a final variable in Java
A var is a regular variable
No "default" mutable - forces you to think about that choice
Scala also has support for immutable data structures
When you add to immutable Map/List/Set in Scala, you get
a new object: Old keys and values are not copied, but
shared
15-May-11 WWW.TIKALK.COM
18. Referential Transparency
The Imperative Way:
Use variables (mutable data) and loops
The Functional Way:
Recursion & higher order functions, without mutable data
Referentially transparent -> Deterministic
Calling a function with a specific set of values as
arguments will always return exactly the same value
15-May-11 WWW.TIKALK.COM
19. Actor Concurrency
The Java thread/monitor model works quite well...
For a small number of threads
Dealing with a very small number of shared objects
But a synchronized method isn't referentially transparent!
Scala supports the Java approach, but it also provides
another model for better scale-up: the Actor model
Message-queue mechanism borrowed from Erlang
A language designed for high concurrency
15-May-11 WWW.TIKALK.COM
20. Actor Mechanics
An Actor is responsible for maintaining data shared by
multiple threads
Only the Actor is allowed to access that data
Threads communicate with the Actor by sending
messages to it
The Actor can respond by sending messages back
If the other thread is an Actor
Typically the messages are immutable
Each Actor has a message inbox where incoming
messages are queued
Scala's Actor library handles the message transfers
The programmer does not have to deal with synchronization
15-May-11 WWW.TIKALK.COM
21. Actors Are Way Better
High-load programs often suffer from:
Data corruption due to concurrent access.
Deadlock
Resource bottleneck or starvation
Java's monitors alleviates the first problem
Scala Also takes care of the second
15-May-11 WWW.TIKALK.COM
22. Actor Libraries
There are 6-7 libraries in Java that implement the actor
model (and 6 in Scala)
Looking for a place to Start? Try Akka
Scala has no benefit, other than being more inherently
focused on distributed/parallel programming
Version 2.9.0 has parallel collections, actors are built in
Scala research group got a 2.3M Euro grant recently to
develop a simpler programming model for parallel apps
15-May-11 WWW.TIKALK.COM
24. Companies that Use Scala
There are more - http://www.scala-lang.org/node/1658
15-May-11 WWW.TIKALK.COM
25. Scala Frameworks
Akka:
an alternative approach to JEE, with distribution support and
several alternatives for concurrency models
Lift:
Full stack for web applications
Scalate:
Templating framework
Play framework:
Moving to Scala
flockdb:
a distributed fault tolerant graph database by twitter
specs, scalatest, scalacheck:
testing frameworks for scala that go beyong junit
15-May-11 WWW.TIKALK.COM
26. Build / Development Environment
sbt:
A build tool that uses Scala for its build files and has
great integration with various frameworks
maven-scala-plugin:
supports compilation, testing, documentation
IDE Support:
Eclipse, IntelliJ, NetBeans
15-May-11 WWW.TIKALK.COM