Tuesday, October 2, 2012

JavaOne 2012: Scala Tricks

The first session I attended after lunch was Venkat Subramaniam's "Scala Tricks," held in the Hilton Golden Gate 3/4/5 conference room area. This is the first session in which I switched my planned session during JavaOne. I switched it last night as I look at my day's schedule and realized that, although excited about Lambda, I felt like I had the familiarity with Lambda I was looking for at this point (thanks to the Technical Keynote and The Road to Lambda), and felt like I'd benefit more from attending "Scala Tricks" than from attending the previously scheduled session "Jump-Starting Lambda Programming" (which sounds packed anyway). Although some speakers prefer questions and comments be asked at the end, Subramaniam prefers that questions be asked when they arise.

Subramaniam began with a quick summary of the basics of the Scala programming language. He showed that everything is an object in Scala by showing the Groovy/Ruby-like example 1.toString(). He stated that you can use Scala anywhere you use Java and should expect 40 to 60 percent less code than you'd write in Java. He added that, like Groovy, you can mix Scala and Java. He likes the conciseness and expressiveness of Scala.

The approach used in this presentation was not to necessarily provide a tutorial on basics of Scala, but rather to approach introduction of Scala via examples. In many ways, this felt like a presentation equivalent of a "recipes" or "cookbook."

Subramaniam's first real example was reading a file (he went for one of Java's ugly points early!): val stuff = Source.fromFile("sample.scala").mkString() [See this StackOverflow thread for differences between toString() and mkString()] There is no signs of exception handling. Like Groovy, Scala does not enforce catching or re-throwing/stating checked exceptions. Removing exception handling makes the code much more concise.

Subramaniam stated that "one of the things I really like about Scala is there is no ceremony to deal with." He described "ceremony" as the stuff you have to do before you can do the stuff you want to do. He pointed out that people teaching Java to someone else are reminded of how difficult Java can be to learn because there are so many aspects of ceremony to learn.

The next example Subramaniam showed was returning multiple values from a method. In this case, he wanted to return two names: "James" and "Bond." He showed creation of an immutable Tuple:

def createAgent() =
   ("James", "Bond")

He showed how the above Tuple could be accessed.

val agent = createAgent()
println(agent._1 + " " + agent._2)

Subramaniam showed what happens when he tried to access agent._3 in the code above: it led to a compile-time error because there aren't three fields. This was an example of a runtime error being moved to a compile-time error.

One of the Scala features that Subramaniam focused on that I also like is its requirement that method arguments be specified as mutable or immutable (val or var) when writing the signature. I liked the var modifier clear back when programming and hacking with Pascal and am glad to see this differentiation.

Subramaniam covered a "very convenient feature" that is on the borderline between "really cool" and "really scary": the implicit keyword. It can, of course, be difficult to know when implicit keyword kicks in, especially if it comes from an import. Subramaniam joked that its use helps improve "job security."

Another example was based on implementing a factorial method in Scala. As is almost always the case, the factorial was implemented with recursion. Subramaniam forced it to recurse enough times to lead to a StackOverflowError. He cited Structure and Interpretation of Computer Programs and then talked about how the code could be written in Scala so that it appears to be recursion, but the Scala compiler will optimize it to be a procedural loop. The @scala.annotation.tailrec annotation can be used to enforce that tail recursion applies, allowing for this compile time optimization to occur. This is pretty nifty, but it's also a reminder that recursion should not be used solely because it's cool.

Subramaniam stated that "Scala says XML should be treated with the respect it deserves" rather than forcing XML content into Strings. He then showed code in which a variable was assigned to an XML element directly without quotation marks. He showed how use of curly braces around that variable assigned directly to XML allows access to the body of that XML element. He said there is much more to Scala's XML support.

"Producing code that spits out XML should be given to people in prison" was Subramaniam's opening statement to introduce Scala's support for generation of XML. He showed how he could embed Scala code within curly braces within the defined XML. This allowed him to define a function that would use Scala trickery to easily build XML based on an underlying Map of data. That is impressive!

Traits are probably one of the most heralded aspects of Scala and Subramaniam covered them in his presentation. He stated that one of the problems with multiple inheritance was C++'s implementation of it. He talked about method pollution and showed how use of the with keyword, coupled with the extends keyword and trait keyword, supports traits.

Subramaniam showed how traits can be applied to individual instances rather than to a general class when so desired (when the instance is different than the general class). His example here was that dogs are friends and so need the trait at the class level while most cats are not friends and so he modeled his cat's individual instance with the friend trait rather than applying the fried trait to the Cat class.

Subramaniam stated that he'd not let anyone use Java who had not first read Effective Java. He said that Java has been fixed to meet the approaches described in Effective Java and that version described in Effective Java is known as Scala.

Subramaniam pulled off well one of the most challenging things I've seen in presentations. Most speakers who turn to their IDE quickly lose the audience as they lose themselves in the code and start talking to the monitor. Subramaniam, however, did a nice job of keeping the code samples small and displayed them with large font. This allowed for easy focus on the important things. His is one of the few code-heavy presentations I've seen that is easy to stay attentive to. The only downside is that these examples are not in the slides, but he said that they'll be available for download.

This was another presentation that was everything I had hoped for. I'm becoming motivated to play around with Scala again and picked up some nice ideas. It didn't hurt that Subramaniam is a skilled and practiced speaker and entertains his audience while educating them. Some speakers, even at this JavaOne 2012, have required extra effort on the audience's part to remain attentive, but that wasn't the case here.

1 comment:

Unknown said...

Thanks for the summary. Subramaniam speaks about many of these concepts in this video as well: https://www.youtube.com/watch?v=LH75sJAR0hc