Scala The Lost Legend of Android

Shubham Soni
3 min readMar 16, 2018

I know everyone is talking about Kotlin and how things are changing when we use kotlin but today I’ going to get you a quick look about how things could be if we use Scala instead of Java in Android.Moving away from a well established and mature language such as Java requires some pretty good reasons. Before pragmatically trying out some of the apparent differences between Java and Scala in order to get acquainted with the Scala syntax, we are going to clarify what makes Scala so attractive.Working with Scala on Android is a real joy. Its syntax helps in writing less code, and cleaner code.

Scala surfaced to the general public in the year 2004. Developed by Martin Odersky, Scala was released on the Java platform as a general-purpose programming language. The name Scala is not some whimsical notion, it is an abbreviation of “scalable”: a language that is scalable according to the needs of its users.

Full Support for Pattern Matching, Macros, and Higher-Kinded Types

Pattern matching in Scala is no joke. Scala uses the match statement for this purpose, which is a powerful version of Java’s switch statement. It allows you to match on any type of data, lists, and even your own types. If you haven’t already tried it, I suggest you play around with it a little.

Write Once, Run Everywhere

Scala is a little complex, but it has a very flexible code syntax. The proficient use of functional programming features makes the code extensible and hierarchical to the very best level.

Bigger Community Support

Scala has been here for more than a decade. Since it is a powerful Java alternative, it has huge support community. We’re talking about YouTube tutorials, blog posts, forums, and let’s not forget the official support and user documentation. Anything you find yourself struggling with, you can pretty much find the answer to it.

Operator Overloading

Scala does not restrict you from overloading operators. You can play with operator-looking functions and define limitlessly. But you need to be careful with this feature. Still, if you can do this right, your code readability will increase. If not, you might end up making your code difficult to understand.

Use implicit type conversions

Scala offers us a very powerful tool. By declaring a function as implicit, it will be used by the compiler to directly convert a type to another.

Take a look at the following code, setting up an OnClickListener for a button:

var button=findViewById(R.id.button).asInstanceOf[Button]
button.setOnClickListener(new View.OnClickListener){
def onClick(v:View) {
Toast.makeText(this, "Clicked", Toast.LENGTH_LONG).show()
}
})

Yes, we need to create a new instance of the OnClickListener and override its onClick method. Doing that for each and every button is cumbersome.

With implicit conversions, we can get a much better syntax. First, let us define a Helpers object:

import android.view.View
objectHelpers{
implicit def onClick(handler: View => Unit):View.OnClickListener = new View.OnClickListener(){
override def onClick(source: View) = handler(source)
}
}

We wrote a method to convert a function taking a View as argument and returning nothing, to an OnClickListener instance. What can we do with that? We can now change the code like this:

importHelpers._
[]
valbutton=findViewById(R.id.button).asInstanceOf[Button]
button.setOnClickListener((v : View) => { Toast.makeText(this, “Clicked”, Toast.LENGTH_LONG).show()
})

Isn’t that cleaner? And you can apply the same idea to other listeners, like OnLongClickListener.

Although its completely on you what you want to use in your code.Yes,I agree if you want to go simple and easier then definetely go with kotlin but for places where you need to handle large amounts of data or complex modeling reliant on mathematics, or simply if you need more power with cleaner code then, Scala would be best.

--

--

Shubham Soni

Senior Executive @Ketto @Dart @Flutter @Java @Android, Editor @FlutterCommunity @CodeBurst.io, App Developer @Senior @Moderator @FlutterDeveloper