What is Scala?
This is how Scala official website introduces the language to the world. In this blog, I will try to make this introduction some way simpler with a taste of real Scala examples, note that I’m assuming a general knowledge of other object-oriented languages like Java or C# but no functional programming knowledge is ok.
Scala is object-oriented
Scala is a pure object-oriented language in the sense that every value is an object. It’s even more object-oriented than Java, by that I mean that any value, even integers and doubles are objects! this opens a new world of no-special treatment for primitive values.
Scala is functional
Scala is also a functional language in the sense that every function is a value. That maybe sounds weird or even crazy, but it opens a whole new world and programming style/paradigm that Scala supports, which is the functional programming world. This is not a place to introduce functional programming style and benefits, but I will mention what help us here. Let’s come back to the fact that every function is a value in Scala, that’s simply mean you can use a function anywhere you can use a value. Ummmm, a function?
A Method or a Function?
To be closer to understand what the functions are, we want to quickly compare them with methods.
- A method, in general, is a piece of code that does something and attached to a context, i.e, belongs to a class.
- While a function is simply a verb that does something or performs an action.
In Scala, we can generalize (or converts) methods to functions, so we can use them outside the class they belong to!
Which is better?
The one should not treat OOP and FP as 2 worlds who should use one of them, even that’s possible in some cases, but to combine them both and use each of them when he/she/they needs to, and that’s exactly why Scala is found.
In general, most of the programmers use OOP as the main paradigm and FP when they need to, which in most cases make the code simpler, shorter, and easier to understand and maintain.
Now, let’s start to discuss Scala.
I will start with you, Java developers, because we share a lot together, we use the same base, yes, the JVM. That’s an incontestable point since JVM is mature enough, optimized enough, and have been in use for over 15 years. You have a guaranteed very easy process to deploy and maintain applications, with a great performance!
2. OOP and FP Fusion
The second point, which is that you have the best of both worlds! why should you use Java and stuck to OOP* or use Haskell and stuck to FP while you can get them together? Ok, I know that Java 8 introduced a lot of features to support FP, but that nothing compared to what Scala offers. May this seems to be a normal point for you but believe me, once you start to taste the FP, you cannot go back.
Let’s have a quick example, given a list of numbers, write a function/method that segregates even and odd numbers into 2 new lists.
Java Solution (Using Imperative Way):
Scala Solution (Using Functional Programming Paradigm):
3. Concise Code
That’s actually another point to mention for Java developers, Scala will give you a more concise and short version of your code (some people will refer to Scala as a better Java!), let’s take another example of defining a simple class with basic methods in both languages:
Yes, that’s it! and actually, the Scala class above explicitly contains even more methods than what I wrote on the equivalent Java one, how great is that?
4. A lot of Features!
Scala is a step ahead of Java, just think of lambda expressions on Java 8, (limited) var and type inference on Java 10, and many other (new) Java features. They are in Scala since day 1, with much more use cases for them.
5. Statically Typed Language
“Dynamically-typed languages perform type checking at runtime, while statically typed languages perform type checking at compile time. This means that scripts written in dynamically-typed languages (like Groovy) can compile even if they contain errors that will prevent the script from running properly (if at all). If a script is written in a statically-typed language (such as Java) contains errors, it will fail to compile until the errors have been fixed”
BTW, Groovy is another JVM language.
Multiply 2 Numbers in Python:
Multiply 2 Numbers in Scala:
6. Type Inference!
Scala has type inference, that’s mean you still may not include the type of the value and let the Scala compiler infer it for you! just print
val x = 5 and you are ready to go!
To be complete, there are some very rare cases where you have to declare the type yourself.
So Scala is statically typed, and you can not write the type yourself, how amazing is Scala!
7. A Scripting Language!
For Python users, a lot of you like the language to write quick scripts. And again, Scala can be used as a scripting language! did you read about REPL ?
8. Data Science and Big Data Processing
Scala is one of the main languages used in the Data Science and Big Data Processing fields. Actually, Spark , one of the most popular engines for big data analytics and processing in Java, Python, R, SQL, and Scala is actually written in Scala, so it’s best to use it in Scala!
9. Parallel Programming
Scala comes with multiple built-in features to support parallel programming in which you can use multiple resources (cores) of your computer to run multiple things/processes together or to run one programme in multiple instances so that you can process it faster. There is one course in the resources below that talks about this.
In fact, Akka , another widely used framework in Java, which helps in building powerful reactive, concurrent, and distributed applications more easily in also written in Scala! Doesn’t that tell you something about the language?
***… Scala has a lot more to encourage you to choose it as your next journey. I can’t mention all the reasons in one article, but I guess I mentioned just enough points to let you consider Scala for your next project! ***
Are you aware of some other reasons to learn and use Scala? Please share them in the comments below!
Learn Scala with Me
Check these resources for more reasons to choose Scala:
- Tour of Scala
- Scala vs. Java: Why Should I Learn Scala?
- Why Scala? (by Martin Odersky, the creator of Scala)
- Why Scala? (for Spark)
- Why should I learn Scala?
- This is why you should learn Scala
- Why should I learn Scala?
- Make 2017 The Year You Learn Scala
- A Newbie’s Guide to Scala and Why It’s Used for Distributed Computing
More resources to learn Scala:
- My GitHub repository . Free.
- Rock the JVM
- Functional Programming Principles in Scala by Martin Odersky, the creator of Scala. Free Course.
- Functional Program Design in Scala by Martin Odersky, the creator of Scala. Free Course.
- Parallel programming in Scala Free Course.
- Programming in Scala Book.
- Scala Programming for Data Science Free Course.
- Scala for Java Developers Book.
- More resources .
See you in the next one!