Wednesday, September 2, 2009

Beginning Scala - Part 9: Singleton in Scala

The singleton pattern is one of the easiest patterns (at least to understand), but ended up not so easy to implement in imperative languages like Java & C#! Scala has this concept built into the language, without the complications. Here's how it looks:
object A extends B {
}
There you go! That's singleton for you in Scala :). The keyword object does everything for you.

Beginning Scala - Part 8: Any, package, abstract, Unit, extends & override

Any
Just as Object is the root of all objects in java, Any is the root of all objects in Scala. It is the basic object unit, in scala, from which all other objects are derived.

Package
In Scala, package definition looks like so:
package shape {

// All class definitions and methods and functions go in here.

   abstract class Z {
     def greet() : Unit
   }

   class A extends Z{
     override greet)() = println("A here!")
   }

   class B extends Z{
     override greet() = println("B here!")
   }

   def aMethod = {
   }
}
Unit in Scala is like void in Java. Abstract classes are denoted by the keyword abstract and all methods in an abstract class are automatically abstract. The extend keywords behaves similarly to its Java equivalent, and the override keyword is a means of overriding the default implementation and providing a new one.

Thursday, August 27, 2009

Beginning Scala - Part 7: import, def & seq

import
The scala equivalent of java's import keyword is the same "import". However, the wildcard * used in java is replaced by _ in scala. Here's how an import statement looks like:
import scala.io._

def
This defines a function and looks like so:
def sum(in: Seq[String]) = {
   // The body of the function goes here


}
This defines a function that takes one argument, a sequence of string. In scala, it is not mandatory to indicate the return type. The compiler does it for you. However, it is a good practice to provide it. The body of a function goes in after the '='. It may be enclosed in parentheses '{' '}'. There is no return in the body of the function, and as per the rules, the last evaluated expression is the returned entity. Note: In scala function declaration, the name of the argument comes first, followed by a ":" and then comes the type of the argument. As per the functional programming paradigm, functions have no side-effects, meaning, it returns the same value no matter what, and more importantly, it does not mutate or change the values/state of any other entity/variables.

Seq[String]
Identifies an entity which is a sequence (collection) of String.

Beginning Scala - Part 6: Hello World

and now ... the customary "Hello World" version of Scala:
scala> println("Hello World!!!")
Hello World!!!
Notice, Scala code need not be terminated by a semi-colon (;). You can type this in the interactive session or save in it HelloWorld.scala. The scripts are executed with the commad scala whereas classes are compiled with scalac. The latter creates JVM class files. The file extension for scala programs is .scala.

The println is part of scala's Predef, predefined stuff automatically part of every program, just like java.lang in Java.

Beginning Scala - Part 5: Immutability

This concept is derived from mathematics, where x = a + b, means what it is and cannot be overridden or given a new value, like x = c + d. The same concept is carried forward into functional programming. So, what good does it do?

The biggest contribution of immutability is scalability. Since they are immutable, there are no locks or synchronization to deal with! Anyone, who has done decent amount of programming will acknowledge to the fact that it is among the toughest part of programming and prone to errors and inconsistencies! Since, there are no locks, there are no overheads (time) associated with obtaining, releasing or queueing for locks. 

Hence, it also makes the application faster! It also makes the code less complicated and more predictable and gives the ability to write code that is more intuitive to write, which usually results in an intuitive end-user application experience.