Hi Scala Dudes & Dudettes !!!

Scala is a great programming language: it's elegant, concise, typesafe, object-oriented and functional, and it's compatible with both the JVM and CLR. If you're already interested in Scala or if you want to know a bit more about it, this site is for you.

Feel free to vote and put your comments through DZone.com
            dzone.com

And don't forget that Scala is the next big thing !!!

Step by step Scala Compiler plug-in

In this article I'll try to show how to implement a basic Scala compiler plug-in. My starting point is this excellent article from Lex Spoon which provides the main information required to understand how compiler plug-ins work. It gives a simple example: a static code checker that raises compilation errors as soon as a division by zero occurs (division by the constant 0).

A compiler plug-in is some Scala code that can be injected as a specific phase of the compilation process of a given project and that allows you to access to the Abstract Syntax Tree (AST) corresponding to the Scala source code that is being compiled. Then it's a matter of matching the proper patterns, raise erros and warnings and, if needed, transform the AST (this, I did not try so far...).

Our goal in this article is to implement the so called "mutable-phobia" plug-in which raises errors as soon as a var definition gets detected !!!
Here is step by step how to do this:

Option type, Some and None

The Option type is a very convenient way to avoid checking null pointers.
Here is a classical "null pointer checking" situation:
def getString : String = {
  ...
  "a string"
  ...
  null
}
 
val str = getString
val str2 = if (str == null) "[null]" else str
This code can be replaced by the following one:
def getString : Option[String] = {
  ...
  Some("a string")
  ...
  None
}
 
val str = getString.getOrElse("[null]")
It's using the Option type together with the Some case class and the None object (from Option.scala):
sealed abstract class Option[+A] extends Product {
  ...
}

final case class Some[+A](x: A) extends Option[A] {
  ...
}
 
case object None extends Option[Nothing] {
   ...
}

Duck typing

Imagine that you want to proceed any instance or object that realizes a given contract i.e. that implements a given set of specific methods. The classical approach tells you to create a new abstraction (an Interface in Java, a Trait in Scala) that provides all the required method signatures. For instance:
/**
 * Using a trait
 */

trait Omnipotent {
  def doThis: String
  def doThat: String
}

def doEverything(o: Omnipotent) = {
  println(o.doThis)
  println(o.doThat)
}

object deity extends Omnipotent {
  def doThis: String = "This is done !!"
  def doThat: String = "That is done !!"
  def doTheRest: String = "Everything's done!!"
}

doEverything(deity)
Now, if you want to do the same thing without defining any additional trait, you can use a type definition instead:
/**
 * Using a type
 */

type Omnipotent = {
  def doThis: String
  def doThat: String
}

def doEverything(o : Omnipotent) = {
  println(o.doThis)
  println(o.doThat)
}

object deity {
  def doThis: String = "This is done !!"
  def doThat: String = "That is done !!"
  def doTheRest: String = "Everything's done!!"
}

doEverything(deity)
Compared to the previous example, the most important difference here is that the deity object doesn't extend anything, but is explicitly of type Omnipotent.

Tuple patterns

In this post I'd like to present a basic but useful feature:
When you need to quickly create a method that returns a set of data instead of only one instance of an existing class, you can use tuples (and the corresponding pattern matching).
Imagine that you need to define a method that takes 2 Ints as parameters and returns 2 Ints, without creating an additional class with 2 attributes. You can return a Tuple as done in the following example:

// returns the sum and product of 2 Ints
def sumAndProduct(n:Int, m:Int) = (n+m, n*m)

Thanks to Tuple patterns, invoking this method and getting the sum and product as returned by the function is straightforward:
val (sum, product) = sumAndProduct(5,6)
println(sum) // prints "11"
println(product) // prints "30"
The tuple values can also be accessed this way:
val ret = sumAndProduct(5,6)
println(ret._1) // prints "11"
println(ret._2) // prints "30"
Can't be easier :))

X10 Home Automation using a Scala internal DSL

Scala has been designed to enable the implementation of Domain Specific Languages. This is surely one of its major strength (see DSLs – A powerfull Scala feature for more information).

This article is an attempt to develop a very simple internal DSL dealing with the X10 home automation protocol (as done here on the top of Oslo).

Here is the syntax (not sure that it reflects exactly the X10 protocol, but it's enough for our example ...):
  • on [house_code] [unit_code]
    where [house_code] is a letter between A and P (16 values) and [unit_code] an integer between 1 and 16 or All (for any devices).
  • Idem for the off command
  • It's the same for bright and dim commands except that an additional level parameter is required:
    bright A 12 level 80

job offers: Scala is a plus

Seen here, the CRM Company (a French company) is hiring J2EE developers.
What's different from the other job offers ? It's the first offer that explicitly mentions that knowing a JVM-based language such as Scala is a plus:
"La pratique de langages tiers ciblant la JVM est un plus (Groovy, Scala, JRuby / Ruby on Rails, Rhino)"

Hope to see that more and more (and not even within french offers :)) ...

Google Scala App Engine ??

Announced by Google this week: the Java App Engine is now available. It comes with a new eclipse plugin that allows to manage both the deployment on the App Engine and application development using GWT (version 1.6).
The first obvious question for a Scala Dude is: What about the Scala integration ?
A first answer can be found directly from the google-appengine-java group:
  • Scala works out of the box.
  • Scala Actors do not work, because they are implemented using Threads (which are not supported).
  • The Lift web framework does not work out of the box, because it depends on Actors and JDBC.
Some blogs confirming the limitations listed above: So it's not perfect, but it's good news for the Scala eco-system anyway !!
Note that it seems that Lift will work on the Java App Engine soon (see the comment from David Pollak: "I'll get code checked up next week that makes core Lift work with gae") ...

They have switched to Scala ...

Both the Scala EPFL site and Lambda the Ultimate are talking about this cool information: the Twitter team has turned to Scala recently ... good choice :))

If you follow the links above, you'll find (amongst other things) a nice presentation from Alex Payne (there's another older one here) as well as an interview of the Twitter team (interviewed by Bill Venners).

By the way, there's a rumor about a potential acquistion of Twitter by Google !!! In case it's effective, I hope that Google will continue pushing a lot for Scala (note that Google "have" Lex Spoon already) and develop some cool tools/libraries based on Scala ... Well... I've a first suggestion: the Scala version of GWT i.e. you write everything in Scala and you get your stuff running on a JVM for the server-side and in javascript or even in scalascript (!!!) on the client-side :))

Scala & the Decorator design pattern

As mentioned in my previous article about the Proxy design pattern, the ability to add features to an existing abstraction without involving an explicit extension of this abstraction is basically the purpose of the Decorator design pattern.
So, this article is simply a recap of the way to implement a Decorator using the technique based on implicit conversion.
Syndicate content