Anonymous Inner Class in Scala PRO

Scala allows us to define anonymous inner classes. The syntax is very similar to the one we know in Java. object Demo { def main(args:Array[String]):Unit = { val ob = new MyStack[Int](0) { def data:Nothing = throw new Exception(“empty stack”); } } } abstract class MyStack[T](size:Int) { def data:T; } The following video clip overviews […]

The Nothing Type in Scala PRO

The main usage for Nothing is when specifying the return type for methods which never return normally (e.g. throw an exception when been called). object Sample { def main(args:Array[String]):Unit = { val ob = getMyStack[Int](10) } def getMyStack[T](num:Int) = { new EmptyMyStack[T] } } abstract class MyStack[T](size:Int) { def data:T; } class EmptyMyStack[T] extends MyStack[T](0) […]

Generic Functions in Scala PRO

The Scala programming language allows us to define generic functions. This code sample shows that. object Sample { def main(args:Array[String]):Unit = { val ob = getMyStack[Int](10) } def getMyStack[T](num:Int) = { new MyStack[T](num) } } class MyStack[T](size:Int) { //… } The following video clips overviews this code sample, shows its execution and provides more information.

Overloading Unary Operators in Scala PRO

When overloading an unary operators in Scala we should precede the operator name with ‘unary_’. Not doing so we will get an exception when using it. The following code sample shows that. package il.ac.hit.samples object Program { def main(args: Array[String]) { val ob = new RationalNumber(1,3) val other = -ob println(other) } } class RationalNumber(a:Int,b:Int) […]

Scala Programming HIT October 2012 INFO

During the first semester of the 2012-2013 academic year that takes place in HIT, I deliver an academic advance course for learning Java and Scala. This course is delivered as part of the academic program for BSc in computer science. You can find the google group of this course at https://groups.google.com/forum/#!forum/hit-scala-october-2012. You can find the […]

By Name Parameters in Scala PRO

When calling a function and passing over an argument which is an expression that needs to be evaluated, the expression will be evaluated before the function is invoked and its value will be passed over. This is the default behavior. By adding => in between the parameter name and its type we will defer the […]

Function Type in Scala PRO

Scala allows us to define variables, function parameters and function returned values with a type that is a function. We can even specify the exact signature of that function. import annotation.tailrec object Program { def main(args: Array[String]):Unit = { var func:(Int,Int)=>Int = sum; println(func(4,3)) func = multiply println(func(4,3)) } def sum(a:Int,b:Int):Int = a+b def multiply(a:Int,b:Int):Int […]

Tail Recursive Function in Scala PRO

If the last action a function performs is calling to itself then it is a tail recursive function. When a tail recursive function is executed the computer doesn’t need to keep the memory stack frames. It can use one frame only. Using the @tailrec annotation we can instruct the computer to use one frame only […]

Multiple Lines Expressions in Scala PRO

When having an expression that spans over more than one line the compiler might evaluate the expression while avoiding parts of the expression. We can overcome this problem in two ways. This video clip explains this issue.

Blocks in Scala PRO

The definitions within a block are visible from within the block only. The definitions within a block shadow definitions with the same names that belong to the block’s outer scope. The block has a value just as any other expression. Its value is the value of the last expression it includes. object Program { def […]

Skip to content Update cookies preferences