2. A Next-Gen Architecture "Manifesto"
• There should be a minimal amount of boilerplate
• The most common programming tasks should be
simple
• It should take advantage of permanent hardware
trends:
– Multi-core CPUs – Vertical scalability
– Cloud-computing – Horizontal scalability
• Preference for JVM and statically typed languages
• It should be enjoyable to use
3. Introducing Scala
• Functional
– Functions are first class entities
– Immutability is the default
– Deterministic
– Lends itself to concurrent programming
• But also, object oriented
– Can code like Java if you want, but you’re not supposed
to!!!!
• Very concise – free of much of the boilerplate code
• Type safe – VERY type safe
• Runs on the JVM – Fully interoperable with Java
4.
5. Popularity
• 12th most popular language in the world and
rising quickly
– Java holding at #2, but slipping on the web
– Python = 4, Groovy = 20, Clojure = 21
• Becoming popular at large companies
– Twitter, Amazon, IBM, LinkedIn, LivingSocial, tomtom
• A lot of the same cast of characters
– James Gosling, Rod Johnson et al. are on the board
6. POJOs
Java Scala
public class User { case class User(val userName: String,
private final String userName; val password: String)
private final String password;
public User(final String userName,
final String password) {
this.userName = userName;
this.password = password;
}
public String getUserName() {
return this.userName;
}
public String getPassword() {
return password;
}
// operators
}
7. Overloading and Polymorphism
Java Scala
void doSomething(int x, int y, int z)… def doSomething(x=true, y=true, z=true) = …
void doSomething(int x, int y){ // USAGE
this.doSomething(x, y, true); doSomething(y = false)
}
void doSomething(int x){
this.doSomething(x, true, true)
}
void doSomething(){
this.doSomething(true, true, true)
}
8. Traits
trait Ordered[A] {
/** Result of comparing this with operand that.
* returns x where
* x < 0 iff this < that
* x == 0 iff this == that
* x < 0 iff this > that
*/
def compare(that: A): Int
def < (that: A): Boolean = (this compare that) < 0
def > (that: A): Boolean = (this compare that) > 0
def <= (that: A): Boolean = (this compare that) <= 0
def >= (that: A): Boolean = (this compare that) >= 0
def compareTo(that: A): Int = compare(that)
}
9. Type Safety
Java Scala
// Query for active users whose name // Query for active users whose name
// is "Joe" // is "Joe"
final String nameParam = "Joe" val nameParam = "Joe"
final String hql = "from " + userTable.where(user =>
User.class.getName() + user.name === nameParam and
" where name = :name AND" + user.status === Status.Active))
" and status = :status"
final Query query = createQuery(hql);
query.setParameter("name", "joe");
query.setParameter("status",
User.Status.ACTIVE.name());
return query.list();
10. Pattern Matching
class Animal(name: String, age: Int)
case class Cat(name: String, age: Int) extends Animal(name, age)
case class Dog(name: String, age: Int, val breed: String)
extends Animal(name, age)
val animal: Animal = …
animal match {
case x: Dog =>
println("You have a dog of type " + x.breed) // THIS COMPILES
case Cat(name, age) =>
println("You have a cat named " + name)
println(s"He is $age years old") // Fancypants string interpolation
case _ =>
println("You have some kind of unknown animal")
}
11. Higher Order Functions
Java Scala
// return all photos > 10k // return all photos > 10k
List<Photo> input=Arrays.asList(...); val photos = List(...)
List<Photo> output = new ArrayList(); val output =
for (Photo c : input){ photos.filter(p => p.sizeKb > 10)
if(c.getSizeInKb() > 10) {
output.add(c);
} // OR
} val output=photos.filter(_.sizeKb > 10)
12. Concurrency
Java Scala
// divide the list into two sub-lists
// as determined by some function
val myFunc = …
val (l1, l2) = photos.partition(myFunc)
// but what if myFunc does a big,
// expensive operation?
??? val (l1, l2) =
photos.par.partition(myFunc)
14. It's hard to read
*Especially in the beginning
def + + [B>: A, That] (that: TraversableOnce [B])
(implicit bf: CanBuildFrom [List [A], B, That]): That
• Functional and imperative programming support =
2 languages in 1
• Learning Scala is like learning 2 languages
15. Power comes at the cost of ease of use
"Computer programing for everybody"
-- Guido van Rossum
"My goal is for Scala to be the language of
choice for the smart kids"
-- Martin Odersky
16. Other downsides
• Tool support is poor
– Have to use IntelliJ, Eclipse is unusable
– SBT leaves a lot to be desired
• A lot of advanced features get abused
– Largely caused by an immature community
• Functionality intended to provide backwards
compatibility with Java and/or to ease the
transition bloats the language
17. Conclusion
• Simple > Easy
• Scala is an extremely promising language that
combines the best features of Java with the
best features of functional languages
• It trades easy for simple. This is good!
• It's becoming very popular
• It’s not perfect
Editor's Notes
Deterministic means free of side effectsTwo languages in one means a lot to learn
80% of all .equals methods are implemented incorrectly