Four reasons to try Scala and love it

I’ve been working a lot with Scala in the last few weeks. It is an amazing programming language, to say the least. It’s terse, intuitive, unambiguous. And a real treat when you want to do stuff. It’s the programming language of the future. If only it would get more support and momentum going for it. Plus, the quantity of online resources available is just mind boggling.

Below are my N reasons why Scala is the best JVM language.

Simplicity at its Best

An application can’t be any more simple and terse than the sample below. If you need to test an idea, screw all the ceremony; just type and go.

object HelloWorld extends App {

    println("Hello World!")


Immutability is King

What do you call a variable that isn’t; a value. Scala has two keywords to define either. You’ll soon realise how many of them are immutable values. And not the dreaded mutable variables.

var defines a mutable variable while val defines an immutable value.

val number = 100 // immutable value 
var counter = 1 // mutable variable

While we’re at it, Scala supports type inference. The following is perfectly fine, legit and simple.

object Variables extends App {
  val number1 = 100D
  val number2: Double = 100
  val number3 = number1 + number2

  println(s"$number1 + $number2 = $number3")

Implicit Methods

Ever have one of those days when you wanted a variable of type Square to be assignable to a variable of type Circle. Yeah. me neither. But, you could do it if you wanted to using implicit methods. Those will convert one type to another type without actually calling the method. It’s Scala witchcraft.

As you can see below, the method convertXtoY is implicitly called when assigning a to b.

object ImplicitFunction extends App { 
  case class X(f: Int, g: Int) 
  case class Y(w: Int, y: Int) 

  implicit def convertXtoY(x: X) = Y(x.f, x.g) 

  val a = X(1, 2) 
  val b: Y = a 

  println(s"Y(${b.w}, ${b.y})") 

Class Extension

How often have you been in the middle of an intense coding session? Then you’re about to call a non-existent method on an object; a method that the laws of common sense state should exist. But who ever graced their awe-inspiring wisdom in designing the class? That individual didn’t think it necessary to add that particular functionality. Well, you can finally right that wrong. All without any OOP. But with some trickery.

The trick is to use that Scala witchcraft to implicitly convert a variable into another type. And then calling the method on that new implicit object.

If you look below, a is calling the method of class Z which is possible because Scala does a conversion of value a from X to Z before calling the method. This manner saves a lot of headache when you need to add new functionality without relying on OOP.

object ImplicitFunction extends App { 
  case class X(f: Int, g: Int) 
  case class Z(m: Int, n: Int) { 
    def sum() = m + n 

  implicit def convertXtoZ(x: X) = Z(x.f, x.g) 
  val a = X(1, 2) 
  val c = a sum 
  println(s"Sum: $c") 

Stay tuned for more. Share your ideas as to why you think Scala is so cool.

This entry was posted in scala, software development and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s