A brief introduction to using Option and Either classes

Previously I had written about Companion classes in Scala and there in I had mentioned one of its uses i.e defining the apply method to support object literals. In this post I am continuing from where I had previously left and pick two such Companion classes in Scala API called the Option class and the … Read more

Brief overview of An “object” in Scala

In Java world we are all familiar with the term object and interchangeably use it with the term instance. Both these terms represent initialization of a class with the required parameters. But the same cannot be applied in Scala, because in Scala “object” is a keyword and represents a totally different but somewhat related concept. Its totally different because “object” keyword represents a single instance of that with which it is used. And similar because it still represents some instance.

I have divided the post into different sections namely:

  1. Simple example of object
  2. An example of inbuilt object in Scala library
  3. Companion classes and Companion objects
  4. Companion object with apply method

Read more

Book Review: Scala for the Impatient

The book covers almost all of the concepts in Scala in a very concise format. More emphasis on learning by doing. Lot of exercise problems at the end of each chapter. It is highly recommended to try them out. The concepts have been divided into varying difficultly levels and the chapters have been arranged so … Read more

Partially applied functions in Scala

Before going into Partially applied Functions, let me introduces 2 terms-
Function Literal- This exists in the source code- Something similar to Class definition. So we have

(x:Int,y:Int) => x+y

Function value- When this function literal is assigned to a reference they become function values. The are similar to the Objects created from the class. These are created at runtime

var sum = (x:Int,y:Int) => x+y

So in the above case we can use sum to invoke the method.

println(sum(5,7))

Read more

Brief insight into Constructors in Scala

In the previous post here, I had introduced few concepts related to constructors in Scala. In this post I would go a bit deeper into the constructors and how we can provide different constructors. Summarizing about the introduction in the previous post here

Read more

Object Oriented Programming in Scala- 1

Scala uses both Object oriented and functional programming concepts. For folks coming from the Java, we would always explore how to do OOP in Scala. Here I thought of writing few posts related to OOP in Scala (may be a Series of posts ;)). In this post lets talk about-

  • Classes
  • Objects
  • Constructors
  • Overloaded Constructors

Read more

Null, null, Nil, Nothing, None, and Unit in Scala

Null– Its a Trait. null– Its an instance of Null- Similar to Java null. Nil– Represents an emptry List of anything of zero length. Its not that it refers to nothing but it refers to List which has no contents. Nothing is a Trait. Its a subtype of everything. But not superclass of anything. There … Read more

Traits in Scala- Deep Dive

Traits are units of code reuse in Scala. Traits encapsulates methods and field definitions. Their role is similar to that of interfaces in Java- A workaround for Multiple Inheritance. But unlike Interfaces they can have method and field definitions. More features will be explained in the article.

Defining Traits:

A trait definition looks like a class definition but uses the keyword “trait.

trait Bounceable{

     def bounce():String="Bounce Method in Trait"

}

The above declared trait has a concrete method. The ability to declare concrete methods in an trait gives default implementation option. In interfaces one has to write/copy-paste the default declaration in each class which implements the interface or declare a class that implements that interface and let other classes extend the concrete class. The first method has code repition and the second method has the restriction that a class can extend only one class. So this feature of “trait” stands out best.

As in Java a class implements interface, in Scala classes traits are mixed into a class. This is done using either extends or “with” keywords.

class Ball(s: Int) extends Bouncable{

      var size:Int

      override def toString():String= Integer.toString(size)

}

In the above example we are using extends to mix in the trait- Here Ball is implicitly inherits trait’s superclass which is “AnyRef”. Lets try to use the class Ball to test the mixed in trait.

object Test{

           def main(args:Array[String])={

                    var ball = new Ball(33)

                    var ball2:Bounceable= new Ball(400)

                    println(ball.bounce()) //This prints "Bounce Method in Trait"

                    println(ball) //This prints "33"

                    println(ball2.bounce())

                    println(ball2)

         }

}

Note: Output written as comment.

Regarding the ball2 instance being declared as type “Bounceable”. Its a valid declaration cause supertype references can refer to the instances of its subtypes. But they cannot access the methods/fields declared by the subtypes (different with the overriding case).

Read more

How’s Scala different from Java?

Scala is statically type like Java but with Type Inferencing support. Which means that the scala compiler analyzes the code deeply to determine what type a particular value is. In Scala its not required to use semicolons to terminate a statement if the termination is obvious by line ending. If there are more than one … Read more

Control Structures in Scala- A brief overview

If Statements: Lets consider a first entry example of If statments, without using much of Scala’s features. if(5+6==10){ println("You are wrong in calculations") }else{ println("Keep it up") } This is a pretty simple and straight forward examples. Now lets add some Scala flavor in the If-Statement. In Scala If-Statements are expressions, which means that the … Read more