1. Scala - just good
for Java shops?
Sarah Mount @snim2
2. Outline
● Scala - who, what why?
● Basics and idioms
● Scala swing
● Concurrency model
● XML
● Advantages and drawbacks
3. Scala - what and why?
● An object-functional language
● Based on earlier work by Odersky and
Typesafe
● Compiles / runs on JVM
● A “better” Java
● Whereas OCaml feels more FP + OO, Scala
is more OO + FP
4. Toolchain
Much like Java:
$ scalac ex1.scala
$ ls
ex1.scala HelloWorld.class
HelloWorld$.class
HelloWorld$delayedInit$body.class
$ scala HelloWorld
Hello, World!
7. Look ma, no semicolon!
object Fibonacci {
def fib(n : Int) : Int = {
if (n == 0 || n == 1) 1
else fib(n - 1) + fib(n - 2)
}
def main(args : Array[String]) : Unit = {
println(fib(5))
}
}
8. Look ma, no constructor!
class Module(val name : String, val code : String) { }
object Ex4 {
def main(args : Array[String]) : Unit = {
val m = new Module("Concurrent Programming",
"5CS004")
println(m.name)
}
}
9. Variables can be ...
val x = 3 // Immutable
var y = 4 // Mutable
lazy val z = 5 // Lazy, immutable
10. Scala Swing
import swing._
object MySwing extends SimpleSwingApplication {
def top = new MainFrame {
val button = new Button { text = "Click me!" }
title = "Hello Swing!"
visible = true
preferredSize = new Dimension(500, 500)
contents = button
}
}
11. XML support built in
import scala.xml._
import scala.io.Source
import java.net.{URL, URLConnection}
import scala.collection.mutable.{Queue, HashMap}
object ex7 {
def main(args : Array[String]) : Unit = {
val url = new URL("http://feeds.bbci.co.uk/news/technology/rss.xml")
val conn = url.openConnection
val rss = XML.load(conn.getInputStream)
for(t <- rss "title") { println(t.text) }
}
}
12. Actor model built in
class Pong extends Actor {
var ping : Ping = null
def act() = {
loop {
react {
case counter : Int =>
println("Pong: " + counter)
reply(counter + 1)
}
}
}
}
class Ping(counter : Int, pong :
Pong) extends Actor {
def act() : Unit = {
pong ! counter
loop {
react {
case counter : Int =>
println("Ping: " + counter)
pong ! (counter + 1)
}
}
}
}
13. Actor model built in
import scala.actors._
import scala.actors.Actor._
object ex8 {
def main(args: Array[String]) : Unit = {
val pong = new Pong()
val ping = new Ping(0, pong)
ping.start()
pong.start()
}
}
14. Scala: The good parts
● Sensible concurrency model
● Has a REPL and can be used as a scripting language
● Fits well with existing Java code
● Boilerplate-free Java
● Statically typed with some inference
● XML handling built into language
● FP constructs - pattern matching, etc.
15. Scala: The not so good parts
Ecosystem moves fast, not always backwards
compatible. This year scala.swing was moved.
Swing examples now compile with:
$ scalac –classpath $CLASSPATH:/usr/share/java/scala-swing.jar *.scala
This year was a good year!
16. Scala: The not so good parts
● Smaller online community, StackOverflow
etc
● The compiler is slow. Like, make a coffee
and drink the whole pot slow
● A caching compiler fsc exists, but
sometimes needs resetting
● Some nice FP features have ugly syntax…
17. Scala: The not so good parts
def aFunction(a : Any) : Unit = {
a match {
case i : Int => println("Integer")
case s : String => println("String")
case d : Double => println("Double")
case _ => println("Wildcard")
}
}