On Scala Or: Why Static Typing Doesn't Have to Suck
Your Designated Presenter Units <ul><li>Michael Neale </li></ul><ul><li>  </li></ul><ul><ul><li>Employed by Red Hat </li><...
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 <ul><li>&quot;I want a mapping of String keys to Integer values&quot; </li></ul>
What We Have To Say in Java <ul><li>HashMap<String, Integer> map =  new  HashMap<String, Integer>(); </li></ul>
What We Can Say in Scala <ul><li>val  map =  new  HashMap[String, Int] </li></ul>
What we want to say &quot;I want stuff to happen when the user click a button&quot;
What we have to say in Java <ul><li>import  javax.swing.JButton; </li></ul><ul><li>import  java.awt.event.ActionListener <...
What we can say in Scala <ul><li>import  scala.swing.Button </li></ul><ul><li>import  scala.swing.events.ButtonClicked  </...
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 <ul><li>&quot;Both a house and a car can be sold, but are not related.&quot; </li></ul>
What we say in Ruby <ul><li>class  House;  def  value; 400000;  end ;  end </li></ul><ul><li>class  Car;  def  value; 3000...
What we say in Scala <ul><li>class  House {  def  value = 400000 } class  Car {  def  value = 30000 } class  BankAccount( ...
The best of both worlds?
Other cool Scala stuff
Actors for concurrency <ul><li>val  actor = actor { loop { receive { </li></ul><ul><li>    case Message(m) => ... </li></u...
Actors for concurrency
Pattern matching <ul><li>val  v = something match { </li></ul><ul><li>   case  Something(s) => whatever(s) </li></ul><ul><...
Pattern matching
Implicits <ul><li>implicit def  extendClass(c: LegacyClass) = EnhancedClass(c) </li></ul><ul><li>//demo ! </li></ul><ul><l...
Functional Programming <ul><li>closures/lambdas  </li></ul><ul><li>(i: Int, s: String) => println(s + (i + 1)) </li></ul><...
Functional Programming <ul><ul><li>fold* </li></ul></ul><ul><ul><li>map </li></ul></ul><ul><ul><li>filter </li></ul></ul><...
Functional Programming
Dynamic language-like? <ul><li>Duck typing => Structural typing </li></ul><ul><li>Monkey patching => Implicit methods </li...
Who is using it?  <ul><ul><li>Twitter </li></ul></ul><ul><ul><li>Foursquare </li></ul></ul><ul><ul><li>EDF Trading </li></...
Questions?
Upcoming SlideShare
Loading in …5
×

On Scala Slides - OSDC 2009

1,648 views

Published on

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

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,648
On SlideShare
0
From Embeds
0
Number of Embeds
47
Actions
Shares
0
Downloads
37
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

On Scala Slides - OSDC 2009

  1. 1. On Scala Or: Why Static Typing Doesn't Have to Suck
  2. 2. Your Designated Presenter Units <ul><li>Michael Neale </li></ul><ul><li>  </li></ul><ul><ul><li>Employed by Red Hat </li></ul></ul><ul><ul><ul><li>Works on various jboss projects </li></ul></ul></ul><ul><ul><ul><li>Talks a lot </li></ul></ul></ul><ul><ul><li>Developer behind Drools </li></ul></ul><ul><li>  </li></ul><ul><li>Thomas Lee </li></ul><ul><li>  </li></ul><ul><ul><li>Works for Shine Technologies </li></ul></ul><ul><ul><ul><li>Groundskeeper </li></ul></ul></ul><ul><ul><li>Sporadic open source contributor guy </li></ul></ul>
  3. 3. On Java ...
  4. 4. The Language Is Like, So 1991
  5. 5. But The VM is Okay
  6. 6. And there are libraries for everything
  7. 7. On Scala ...
  8. 8. What We Want To Say <ul><li>&quot;I want a mapping of String keys to Integer values&quot; </li></ul>
  9. 9. What We Have To Say in Java <ul><li>HashMap<String, Integer> map = new HashMap<String, Integer>(); </li></ul>
  10. 10. What We Can Say in Scala <ul><li>val map = new HashMap[String, Int] </li></ul>
  11. 11. What we want to say &quot;I want stuff to happen when the user click a button&quot;
  12. 12. What we have to say in Java <ul><li>import javax.swing.JButton; </li></ul><ul><li>import java.awt.event.ActionListener </li></ul><ul><li>import java.awt.event.ActionEvent </li></ul><ul><li>JButton button = new JButton(&quot;Click Me!&quot;); </li></ul><ul><li>button.addActionListener( new ActionListener(){ </li></ul><ul><li>  public void actionPerformed(ActionEvent e) { </li></ul><ul><li>    // stuff(); </li></ul><ul><li>  } </li></ul><ul><li>}); </li></ul>
  13. 13. What we can say in Scala <ul><li>import scala.swing.Button </li></ul><ul><li>import scala.swing.events.ButtonClicked </li></ul><ul><li>  </li></ul><ul><li>val button = new Button(&quot;Click Me!&quot;) </li></ul><ul><li>button.reactions += { </li></ul><ul><li>    case ButtonClicked(e) => // stuff() </li></ul><ul><li>} </li></ul>
  14. 14. But what about Ruby/Python/Perl?!!1!
  15. 15. Scala types are statically checked
  16. 16. Sun JVM: no GIL / green threads / etc.
  17. 17. But Scala is comparably expressive
  18. 18. An example: Ruby and Scala <ul><li>&quot;Both a house and a car can be sold, but are not related.&quot; </li></ul>
  19. 19. What we say in Ruby <ul><li>class House; def value; 400000; end ; end </li></ul><ul><li>class Car; def value; 30000; end ; end </li></ul><ul><li>class BankAccount </li></ul><ul><li>  def initialize(balance); @balance = balance; end </li></ul><ul><li>  def credit(amount); @balance += amount; end </li></ul><ul><li>end </li></ul><ul><li>  </li></ul><ul><li>def sell(acct, asset); acct.credit(asset.value); end </li></ul><ul><li>bank_acct = BankAccount.new(0) </li></ul><ul><li>sell(bank_acct, House.new) </li></ul><ul><li>sell(bank_acct, Car.new) </li></ul>
  20. 20. What we say in Scala <ul><li>class House { def value = 400000 } class Car { def value = 30000 } class BankAccount( var balance : Int)   def credit(balance : Int) = </li></ul><ul><li>    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) } </li></ul>
  21. 21. The best of both worlds?
  22. 22. Other cool Scala stuff
  23. 23. Actors for concurrency <ul><li>val actor = actor { loop { receive { </li></ul><ul><li>   case Message(m) => ... </li></ul><ul><li>   case Another(m) => reply { answer } </li></ul><ul><li>}}} </li></ul><ul><li>actor ! Message(&quot;hello&quot;) </li></ul><ul><li>val answer = actor !? Another(&quot;ola&quot;) </li></ul><ul><li>//its just a library ! other options: STM etc... </li></ul>
  24. 24. Actors for concurrency
  25. 25. Pattern matching <ul><li>val v = something match { </li></ul><ul><li>   case Something(s) => whatever(s) </li></ul><ul><li>   case m: SomeClass => whatever(m) </li></ul><ul><li>   case (42, &quot;hey&quot;) => 42 </li></ul><ul><li>   case _ => &quot;Nothing matched&quot; </li></ul><ul><li>} </li></ul><ul><li>//compiler can warn for non exhaustive match </li></ul>
  26. 26. Pattern matching
  27. 27. Implicits <ul><li>implicit def extendClass(c: LegacyClass) = EnhancedClass(c) </li></ul><ul><li>//demo ! </li></ul><ul><li>//used to enhance stuff </li></ul><ul><li>//use wisely ! </li></ul>
  28. 28. Functional Programming <ul><li>closures/lambdas  </li></ul><ul><li>(i: Int, s: String) => println(s + (i + 1)) </li></ul><ul><li>val func = (i : Int) => ....  </li></ul><ul><li>val nums = (1 to 100) filter (_ < 50) </li></ul><ul><li>def log(message: => String) = .... </li></ul><ul><li>val immutableList = List(1, 2, 3) </li></ul><ul><li>def calcSomething : Option[Result] = ... </li></ul><ul><li>calcSomething match { case Result => ... } //no NPE ! </li></ul>
  29. 29. Functional Programming <ul><ul><li>fold* </li></ul></ul><ul><ul><li>map </li></ul></ul><ul><ul><li>filter </li></ul></ul><ul><ul><li>lazy sequences </li></ul></ul>
  30. 30. Functional Programming
  31. 31. Dynamic language-like? <ul><li>Duck typing => Structural typing </li></ul><ul><li>Monkey patching => Implicit methods </li></ul><ul><li>A shell ! </li></ul>
  32. 32. Who is using it?  <ul><ul><li>Twitter </li></ul></ul><ul><ul><li>Foursquare </li></ul></ul><ul><ul><li>EDF Trading </li></ul></ul><ul><ul><li>Siemens </li></ul></ul><ul><ul><li>... and others! </li></ul></ul>
  33. 33. Questions?

×