My Scala Days [Week 1]
I started doing Scala not because I am curious about it, like Haskell, but because I have to since the project that want to work on is in Scala.
Some articles say Scala means scalable language but one particular article says it mean staircase in Italian. And Scala surely means “ladder” (or staircase) in Italian.
Unfortunately, I don’t have strong Java basics so I thought I would have some problems with it.
Fortunately, I am very comfortable with Functional Programming so that is one less thing to worry about it.
Let’s move on to setting up the development environment and learning part.
Development Environment #
For the development environment, I tried to resist my temptation to setup Emacs for Scala Development. For those who insist on doing everything on Emacs, here are two things that you need for your Emacs; ENhanced Scala Interaction Mode for Emacs and New Scala Mode.
This time, I need to speed things up and I want everything as simple as possible with Get Shit Done Philosophy.
- I installed Scala (For OSX) with
brew install scala
- I downloaded IntelliJ and installed Scala plugin
I started doing a typical Hello World after I have installed and have my environment set up.
There was one problem which I went through when I was trying to get my Hello World program to run in IntelliJ. The problem is that there are multiple 'scala-library*.jar'
files.
Error:scalac: Multiple 'scala-library*.jar' files (scala-library.jar, scala-library.jar, scala-library.jar) in Scala compiler classpath in Scala SDK scala-sdk-2.11.7`
The problem occurs because of the duplicates of library files in the directory that I pointed for Scala SDK in IntelliJ. I pointed to /usr/local/Cellar/scala/2.11.7
for the Scala SDK and there are two folders which contains library files: one folder has the real files and the other has symlinks to the files in the former directory. If you want to make sure of the installed path of Scala, do brew info scala
and you will have some useful info. For my case, I realized that I have to point to /usr/local/opt/scala/idea
for Scala SDK for IntelliJ. So, I set my Scala SDK again in IntelliJ by doing File > Project Structure
and then removing the Scala SDK that is already there (you can see the duplicates on the right panel) and then adding back again by setting the path as /usr/local/opt/scala/idea
.
Things work well after that and there, Lo and Behold, the “Hello World” appears after the program is successfully compiled.
Learning Resources #
The first thing that I did was download all the videos of Functional Programming in Scala and prepared my mind to do all those. That’s going to be super tough for me.
I gathered as many resources for Scala as possible and I got the following:
- Scala for the Impatient [Book]
- Programming in Scala [Book]
- Scala School
- Ninety-Nine Scala Problems – I take this as a learning resource rather than just exercises since doing the exercises teach me a lot about Scala. That’s the approach I did with Haskell too.
- ScalaDocs
- Another Tour of Scala **** #My Learning Process
It is a good idea to read Martin Odersky’s Scala levels guide and put a mark on where you want to be after “x” amount of time. But first, I need to get a hang of what this whole Scala thing is. One thing that I have become after being a Haskeller for quite a while is that I am too lazy to write a lot of code to have something that I want. But well, I have to learn to be a bit verbose again if I want to be in the land of Scala.
First, I read the first two chapters of Scala for the Impatient and tried out the exercises. It is always a good idea to do exercises rather than just reading the book since the real learning happens when you are twisting your brain this way and that way in a foreign land. But I have to admit that I wanted to move on quick so I didn’t give much time for each exercise. I referred to solutions for Scala for the Impatient as I move on.
At the same time, I followed the guide of Learning Scala by Joel Abrahamsson to get my feet wet and to clean my head a bit when I am stuck with those exercises from SftI or just plainly get bored with that.
Another article which really proved to be helpful after getting a little bit warm in Scala is First Steps to Scala by Bill Venners, Martin Odersky, and Lex Spoon. This article is a nicely written and can serve as a good recap after getting here and there in the first week of learning Scala.
Here are some of the takes for me:
- The recommended style guideline for method invocations for methods with side effect is to provide parentheses
- Scala achieves a conceptual simplicity by treating everything, from arrays to expressions, as objects with method
- In Scala, classes can take parameters directly. The Scala notation is more concise—class parameters can be used directly in the body of the class; there’s no need to define fields and write assignments that copy constructor parameters into fields
- In Java, you sometimes give classes multiple constructors with overloaded parameter lists. You can do that in Scala as well, however you must pick one of them to be the primary constructor, and place those constructor parameters directly after the class name
- You can’t have static fields or methods in a Scala class. Instead, Scala allows you to create singleton objects using the keyword object. A singleton object cannot, and need not, be instantiated with new. It is essentially automatically instantiated the first time it is used and there is ever only one instance. A singleton object can share the same name with a class, and when it does, the singleton is called the class’s companion object
- A singleton object is not a companion object when it does not have a class name associated with it. That kind of singleton is called a stand-alone object. Thus, a singleton object is either a companion or a stand-alone object. The distinction is important because companion objects get a few special privileges, such as access to private members of the like-named class
- You can give methods real bodies with real code in Scala traits
- In Scala, a trait can extend zero to many traits and a class can extend zero to many traits
I need to get a better hang of traits.
Here, again, is a very good place to get deeper and deeper into the concepts of Scala by taking Another Tour of Scala. I read the first one Scala Basics and I pretty much liked it so I will continue munching those articles as I continue learning.
I tried to work on Scala Labs and Scala Koans but sbt takes too long whenever I am starting it up. I have no idea how to work on those too. I have to find a way to work on it.
After learning quite a bit of basics of Scala from the above resources, I find Scala cheat sheet very helpful. This cheat sheet clarifies as well as solidifies some of the new concepts as well as the fuzzy ones in my mind
This blog does not encompass all the details of my Scalain Journey. This will be a living and breathing matter as I add more and more stuffs as I remember what I did and what I stumbled upon.