Scala - just good for Java shops?

3,961 views
4,093 views

Published on

Talk given at the first Thames Valley Functional Programmers Meetup on 28th July 2014.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,961
On SlideShare
0
From Embeds
0
Number of Embeds
2,827
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Scala - just good for Java shops?

  1. 1. Scala - just good for Java shops? Sarah Mount @snim2
  2. 2. Outline ● Scala - who, what why? ● Basics and idioms ● Scala swing ● Concurrency model ● XML ● Advantages and drawbacks
  3. 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. 4. Toolchain Much like Java: $ scalac ex1.scala $ ls ex1.scala HelloWorld.class HelloWorld$.class HelloWorld$delayedInit$body.class $ scala HelloWorld Hello, World!
  5. 5. Hello Java! // Access modifiers, classes, names, blocks, naming // conventions. public class Hello { // Access modifiers, “static”, arg types, return types, // methods, argument passing, strings, arrays, // command-line args, blocks, “main”. public static void main(String[] args) { // Classes, objects, methods, literals, // “.”, string literals, sequencing. System.out.println(“Hello Java world!”); } }
  6. 6. Hello Scala! // Access objects, inheritance, // strings, blocks, naming // conventions object HelloWorld extends App { println("Hello, World!") }
  7. 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. 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. 9. Variables can be ... val x = 3 // Immutable var y = 4 // Mutable lazy val z = 5 // Lazy, immutable
  10. 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. 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. 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. 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. 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. 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. 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. 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") } }
  18. 18. Scala: Just good for Java shops? Probably.

×