Sunday, February 27, 2011

Scala implicit conversion

Implicit conversion helps us to convert between types without explicitly calling conversion methods. Let's take a look at following example.

We have a list of names.
val xs = List("John", "Jack", "Boby")
If we assign it to a string value, scala will complain that there is a type mismatch error.
scala> val s : String  = xs  
<console>:6: error: type mismatch;
 found   : List[java.lang.String]
 required: String
       val s : String  = xs

Let's define the implicit conversion here.

implicit def list2String(l: List[String]) = l.foldLeft(""){ (sum, x) => sum + x + (if (x == l.last) "" else ", ")}
and let's try the assignment now.
scala> val s: String = xs
s: String = John, Jack, Boby


It saves a lot time and code. But we have to follow several rules.

Implicit conversion rules
  1. Marking Rule: Only definitions marked implicit are available.
  2. Scope Rule: An inserted implicit conversion must be in scope as a single
    identifier, or be associated with the source or target type of the conver-
    sion.
  3. Non-Ambiguity Rule: An implicit conversion is only inserted if there is
    no other possible conversion to insert.
  4. One-at-a-time Rule: Only one implicit is tried.
  5. Explicits-First Rule: Whenever code type checks as it is written, no implicits are attempted.
Look at Predef methods. It has a number of implicit converters which imported automatically.

Reference:
Programming in Scala by Martin Odersky, Lex Spoon, Bill Venners

Saturday, February 26, 2011

Scala basic data types

Value type Range
Byte 8-bit signed two’s complement integer (-27 to 27 - 1, inclusive)
Short 16-bit signed two’s complement integer (-215 to 215 - 1, inclusive)
Int 32-bit signed two’s complement integer (-231 to 231 - 1, inclusive)
Long 64-bit signed two’s complement integer (-263 to 263 - 1, inclusive)
Char 16-bit unsigned Unicode character (0 to 216 - 1, inclusive)
Stringa sequence of Chars
Float 32-bit IEEE 754 single-precision float
Double 64-bit IEEE 754 double-precision float
Boolean true or false

Data types shown above reside in package scala except String! String is a member of java.lang package and it's automatically imported therefore it's implicitly converted to scala's WrappedString class (it used to convert to RichString before scala 2.8.1). We can call WrappedString's methods on a string. Voila!

Friday, February 25, 2011

Scala Lists

Scala List is one of heavily used data structures in Scala. It's immutable and is designed to enable functional programming style.

Initializing a list
val xs = List(1, 3, 4)

This creates an instance val named xs, initialized with a new List[Int] with the integer elements 1, 3, and 4.

Remember that List is an abstract class so val xs = new List(1, 2, 3) would not work! Then how does this create a list? When we write List(1,3, 4) we are implicitly calling apply method of List companion object. More about companion object and apply method will be another post. For now let's think List.apply as Static object with a factory method.