🇧🇷 Leia em Português

My saga learning Scala - Part 1

A black and white image of a laptop and a notebook faded and on the middle of the image there is the Scala logo in red.

I decided that I needed to learn a new language and the language I choose was Scala. I added as my goal for 2020 to at least feel a bit comfortable with this language, so here I am.

So far, my history with Scala has been very… frustrating (to say the least). I was trying to learn on my own and quiet - because it is so hard - but I decided to write down what I learned and what was hard in the process. That may be helpful for someone else, but mainly this is for me. In here I will force myself to organize my thoughts, share interesting things I found and find some use to my frustrations. These posts are not intended to be a tutorial, but rather a way of brain dump.

Why Scala?

To be honest, I didn’t choose Scala, Scala chose me 😅. In general my reasons were:

  • ✅ It is a language that is both objected-oriented and functional at the same time. This would make me question my assumptions and change my way of code. Every amazing developer I know recommended me to learn a functional language to change my ways of developing. I could choose anyone, Scala might as well be it.
  • ✅ It is a language that’s being highly used for large dataset analysis and thus super useful on data science world. I love data science, I love crunching data. If I want to go to a language that is useful for data science and large data, Scala is it.
  • ✅ It is a strong typed JVM language. Something that can help me introduce this world and go on a different direction than Python.
  • ✅ It is one of the main stacks of my current work. On the past year I had to work on Scala code and it required a lot of help from friends. It is an opportunity to learn something… so why not just face it?

More about the language

From wikipedia:

Scala is a general-purpose programming language providing support for functional programming and a strong static type system. Designed to be concise, many of Scala’s design decisions aimed to address criticisms of Java.

Scala is a compiled language, which means that all the code you write is compiled to a binary file that is processed by the JVM (Java Virtual Machine). The interesting part is that the binary file generated, is the same type of file generated by the Java compiler.

I thought that Scala was a new (2010s) creation but its creation actually started in 2001 by Martin Odersky at was released in 2003. There are some interviews with Martin talking about how he came with the idea of creating a language that was better than Java but still used the Java infrastructure. And this is something that is particularly weird coming from another language rather than Java: you can use Java code and import Java libraries in Scala. But this is not something rare, a lot of things in Scala are actually Java objects and can be called from the default Java libraries. This is amazing if you are trying to change from one to the other, but if you know nothing about Java, I feel that you actually learning two things at once.

I also wanted to see how people are seeing Scala in the market. My natural place was the StackOverflow research they do every year. Since the 2020’s report is still not out yet, I checked the report of 2019. In 2019 Scala was listed as 20th out of the top 25 most popular languages, 58% of developers that work with Scala love the language and is the 17th most wanted language.

Which materials am I using in my study?

First I started looking to some recommended books. Then I bumped into my first problem while trying to move from Python to Scala: All freaking books and tutorials assume I know Java to teach me Scala. And, as you can imagine, I don’t.

The first book someone recommended to me, Functional Programming in Scala, was too confusing and compared everything to Java. Instead of explaining the features, it would go “this is the same as Java”. And that’s it. Great! Now I need to learn a language to learn a language? Wat?

After that I started looking at some online resources. The Tutorials Point was a good way to start understanding variables, a bit of classes and how to write code. But I felt that I was missing so much. I was not satisfied and decided to continue to search for a material that would suit me.

Finally, I found this link of Top Tutorials to Learn Scala and I found a course called Scala & Functional Programming for Beginners by Daniel Ciocîrlan. It felt like a relief and decided to buy it. Th courses doesn’t assume you know Java and explains the ways of thinking and writing code in a more functional way. So far, I am really enjoying it and totally recommend.

I also got some awesome recommendation from the marvelous Dani Petruzalek that I still haven’t seen it all: a Coursera class and book from Martin Odersky, the language creator, and this talk:

Some on the things I learned so far

Types

Scala has types, but they are not enforced by the compiler. So, similar to Python, if I do val x = 2, the compiler will understand that 2 is an Integer and so x is an Integer as well. The same is valid for the types that are returned by functions. Example:

def aFunction(a: Int) = a

// same thing as

def aFunctionExplicit(a: Int): Int = a

This is true except in recursive functions. Recursive functions require that the output to be explicit otherwise the compiler will raise an error.

Smart compiler

The same way the compiler infers the type based on the structure, it can also make changes based on it. For instance, do you think the following code will work?

val x = 2 + " bananas"

Yes it will! The compiler will see that you are adding an Integer to a String and call the function 2.toString to convert the Integer and avoid an error. The same is valid for:

val x = 2 + "3"

Which will return a String “23”. I still don’t know how this is defined, if String has a priority over Integers or not (it appears so), but I’ll try to find out more about this 🙂

A function return the last thing

When you write a function, you don’t need to use return to be explicit on the return value, a function will always return what’s inside a block. In the example below we have a value that is an Integer, but the return of the function is a String.

def aFunction(a: Int) = {
  val x = 123 + 456
  "Something"
}

Lots of things are not enforced!

Parentheses on functions that don’t receive parameters are also not enforced. So "Hello".sorted() is the same things as "Hello".sorted.

The same is valid for brackets to define a function block!

def anotherFunction(n: Int): Int =
  if (n == 1) 1
  else 2

// same thing as

def anotherFunction(n: Int): Int = {
  if (n == 1) 1
  else 2
}

Methods are… different

Methods can be named anything, including special characters such as + or %. And if a function receives only one parameter, you don’t have to add the parentheses. So basically, when you do a + b you are actually calling method + in instance a with b as a parameter!

a + b 

// same thing as

a.+(b)

This is it for now 🙂 See you in Part 2!


Cheers!
Letícia

Comments