Archive for July, 2009
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 are no instances [...]
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 [...]
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 statements [...]
Read Full Post | Make a Comment ( None so far )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 value from If-Statements can be assigned to [...]
Read Full Post | Make a Comment ( None so far )Tuples- Returning multiple values in Scala
When I was coding in Java I used to build Classes just to return multpile values and also sometimes used pass by reference (by means of using Objects). I really missed a permanent solution
Scala has a solution for this- It supports something called “Tuples” which is created with the literal syntax of a comma-separated list of the items inside parentheses like (x1,x2,x3 …). The items in the parantheses may not be related to each other in terms of the type, which means that we can have String’s, Int’s and so on. These literal “groupings” are instantiated as scala.TupleN instances, where the N is the number of items in the tuple. The Scala API defines separate TupleN classes for N between 1 and 22, inclusive. Tuples can be assigned to variables, passed as values or return them from the methods.
“val” versus “var” Declarations in Scala
Scala allows programmers to decide whether the variable is immutable or mutable. This can be done by using the keywords “val” and “var”. Continue reading to know more about “val” and “var”.
Read Full Post | Make a Comment ( None so far )