On Scala Slides - OSDC 2009

Preview:

DESCRIPTION

A presentation by Tom Lee and Michael Neale at OSDC conference, Brisbane, 2009. Why scala sucks less.

Citation preview

On ScalaOr: Why Static Typing Doesn't Have

to Suck

Your Designated Presenter Units

Michael Neale • Employed by Red Hat

o Works on various jboss projectso Talks a lot

• Developer behind Drools 

Thomas Lee • Works for Shine Technologies

o Groundskeeper• Sporadic open source contributor guy

On Java ...

The Language Is Like, So 1991

But The VM is Okay

And there are libraries for everything

On Scala ...

What We Want To Say

"I want a mapping of String keys to Integer values"

What We Have To Say in Java

HashMap<String, Integer> map = new HashMap<String, Integer>();

What We Can Say in Scala

val map = new HashMap[String, Int]

What we want to say

"I want stuff to happen when the user click a button"

What we have to say in Java

import javax.swing.JButton;import java.awt.event.ActionListenerimport java.awt.event.ActionEvent

JButton button = new JButton("Click Me!");button.addActionListener(new ActionListener(){  public void actionPerformed(ActionEvent e) {    // stuff();  }});

What we can say in Scala

import scala.swing.Buttonimport scala.swing.events.ButtonClicked  val button = new Button("Click Me!")button.reactions += {    case ButtonClicked(e) => // stuff()}

But what about Ruby/Python/Perl?!!1!

Scala types are statically checked

Sun JVM: no GIL / green threads / etc.

But Scala is comparably expressive

An example: Ruby and Scala

"Both a house and a car can be sold, but are not related."

What we say in Ruby

class House; def value; 400000; end; endclass Car; def value; 30000; end; endclass BankAccount  def initialize(balance); @balance = balance; end  def credit(amount); @balance += amount; endend def sell(acct, asset); acct.credit(asset.value); endbank_acct = BankAccount.new(0)sell(bank_acct, House.new)sell(bank_acct, Car.new)

What we say in Scala

class House { def value = 400000 }class Car { def value = 30000 }class BankAccount(var balance : Int)   def credit(balance : Int) =    this.balance += balance}

object BankApp extends Application {  def sell(acct:BankAccount, asset:{def value : Int}) =    acct.credit(asset.value)  val bankAcct = new BankAccount(0)  sell(bankAcct, new House)  sell(bankAcct, new Car)}

The best of both worlds?

Other cool Scala stuff

Actors for concurrency

val actor = actor { loop { receive {   case Message(m) => ...   case Another(m) => reply { answer }}}}

actor ! Message("hello")val answer = actor !? Another("ola")

//its just a library ! other options: STM etc...

Actors for concurrency

Pattern matching

val v = something match {  case Something(s) => whatever(s)  case m: SomeClass => whatever(m)  case (42, "hey") => 42  case _ => "Nothing matched"}

//compiler can warn for non exhaustive match

Pattern matching

Implicits

implicit def extendClass(c: LegacyClass) = EnhancedClass(c)

//demo !

//used to enhance stuff

//use wisely !

Functional Programming

closures/lambdas 

(i: Int, s: String) => println(s + (i + 1))

val func = (i : Int) => .... 

val nums = (1 to 100) filter (_ < 50)

def log(message: => String) = ....

val immutableList = List(1, 2, 3)

def calcSomething : Option[Result] = ...calcSomething match { case Result => ... } //no NPE !

Functional Programming

• fold*• map• filter• lazy sequences

Functional Programming

Dynamic language-like?

Duck typing => Structural typing

Monkey patching => Implicit methods

A shell !

Who is using it? 

• Twitter• Foursquare• EDF Trading• Siemens• ... and others!

Questions?

Recommended