Scala In The Wild

3,357
-1

Published on

Published in: Technology, News & Politics

Scala In The Wild

  1. 1. Scala in the Wild Daniel Spiewak
  2. 2. Vanity Slide • Software Developer at Novell • Working on a next-gen communications system called “Pulse” • Author of Scala for Java Refugees • ...and a fair bit more • An unhealthy fascination with languages
  3. 3. Brief Overview of Pulse • Next-Gen communications platform • Implemented as a web application • AJAX • Comet • Focus on security and enterprise management
  4. 4. Architecture Messaging Service Search Service (SOLR) Webapp (Lift) RabbitMQ Notification Service WFP Service HBase + MySQL
  5. 5. Nifty Tricks • Messages are chunks of XML
  6. 6. <message id="42"> <line/>Playing with Pulse (and trying to avoid accidentally poluting any of Andy's demos). <message id="43"> <line/>We can reply quite easily. </message> </message>
  7. 7. Nifty Tricks • Messages are chunks of XML • Extend Elem • Custom NoBindingFactoryAdapter • Use #load(String)
  8. 8. Nifty Tricks • Messages are chunks of XML • All frontend services use Cake Pattern • Very extensive use of actors
  9. 9. Challenges • Actors require a recovery mechanism • Java / Scala interop is a little clumsy • Collections conversion (java-utils)
  10. 10. import scala.collection.jcl.Conversions._ def foo(bar: java.util.List[String]) = { val xs: Seq[String] = bar bar.add("baz") xs foreach println // does it contain "baz"? } def foo(bar: List[String]) = { val back = new java.util.ArrayList[String] bar foreach back.add back }
  11. 11. import org.scala_tools.javautils.Implicits._ def foo(bar: java.util.List[String]) = { val xs = bar.asScala bar.add("baz") xs foreach println } // returns java.util.List[String], and in O(1) too! def foo(bar: List[String]) = bar.asJava
  12. 12. Challenges • Actors require a recovery mechanism • Java / Scala interop is a little clumsy • Collections conversion (java-utils) • Spring doesn’t like mixins • Tools are primitive (at best) • Scala devs are in short supply
  13. 13. Java Refugees • Tendency to write Java in Scala
  14. 14. class Person(fn :String, ln :String) { private var theFirstName :String = fn; private var theLastName :String = ln; def getFirstName() :String = { return theFirstName; } def setFirstName(fn :String) :Unit = { theFirstName = fn; } // ... override def equals(obj: Any) :Boolean = { if (obj.isInstanceOf[Person]) { var p :Person = obj.asInstanceOf[Person]; return p.getFirstName() == theFirstName && p.getLastName() == theLastName; } return false; } }
  15. 15. class Person(var firstName: String, var lastName: String) { override def equals(a: Any) = a match { case that: Person => { this.firstName == that.firstName && this.lastName == that.lastName } case _ => false } }
  16. 16. Java Refugees • Tendency to write Java in Scala • Failure to exploit advanced features • Nested imports • Point-Free / Currying • Monads
  17. 17. def foo(bar: Bar) = { var result: Baz = null Box.!!(bar).foreach(person => Box.!!(person.name).foreach(name => Box.!!(name.find("blah")).foreach(result = _))) Box !! result }
  18. 18. def foo(bar: Bar) = { for { person <- Box !! bar name <- Box !! person.name result <- Box !! (name find "blah") } yield result }
  19. 19. Java Refugees • Tendency to write Java in Scala • Failure to exploit advanced features • Nested imports • Point-Free / Currying • Monads • Higher-Kinds only raise confusion • Arbitrary file organization
  20. 20. Solution: Conventions! http://davetron5000.github.com/scala-style/
  21. 21. Scala Style • Inspirations • Java • Standard ML • Haskell • C# • OCaml • Ruby • Python
  22. 22. Scala Style • Leverage type inference! val s: String = "blah" // wrong val s = "blah" // right
  23. 23. Scala Style • Leverage type inference! • Higher-Order functions // wrong def foldLeft[A](init: A, f: (A, B) => A) = ... // right def foldLeft[A](init: A)(f: (A, B) => A) = ... foldLeft(0) { (a, b) => ... }
  24. 24. Scala Style • Leverage type inference! • Higher-Order functions • Function values foo (x) => x + 1 // wrong foo (x) => { x + 1 } // wrong foo { _ + 1 } // right foo { x => x + 1 } // right
  25. 25. Scala Style • Leverage type inference! • Higher-Order functions • Function values • Correct use of implicits • Pimp-my-library • Correcting inheritance (typeclasses)
  26. 26. Scala Style • ... • Function values • Rules for correct use of implicits • Arity-0 methods println // wrong println() // right xs.length() // wrong xs.length // right
  27. 27. Scala Style • Leverage type inference! • Higher-Order functions • Function values • Rules for correct use of implicits • Arity-0 methods • File organization
  28. 28. Conclusion(s) • Yes, Scala is ready for the enterprise! • ...but there’s definitely room to improve • Process and conventions are critical • Community is developing standards. Participate!
  29. 29. Thank You
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×