Revisiting Scala
I’ve been feeling ill the last couple of days and haven’t been able to face writing, but today I’m a lot better, so that means it’s time to write! I’m going to take a break from OCaml for a little bit - to tell the truth I was mostly interested in using F# and OCaml was more something to help with learning as F# and OCaml are very similar. Today I’m going to look at Scala again which should be more fun as I’m a Java programmer!
The state of Scala
As of the time of writing (25/06/2015) Scala is currently on version 2.11.7 and has come a long way since I last had a look at it. I’m mostly interested in using Scala to create GUI applications and so that’s what this post will focus on.
In Java 8 JavaFX was introduced as a replacement for the aging Swing framework. If you’ve worked with Swing before I’m sure you can see why a replacement is long overdue. Since I’m interested in both functional programming and GUI programming this is a perfect opportunity to try out both Scala and JavaFX (via ScalaFX) at the same time and learn a little about both.
Installation
I’m not going to cover this in much depth as there are plenty of resources on the internet. I installed the latest version of Scala (2.11.7) as mentioned above, and then installed SBT. To write Scala code I’m using IntelliJ with the excellent Scala plugin.
Part of the reason for moving away from OCaml (for now anyway) was the lack of tooling - or at least tooling which would be responsive enough (I’m looking at you eclipse) on the laptop I am currently using.
The hello world code from the ScalaFX code was useful and shows how GUI’s are constructed. The code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
The result looks like this:
Simple enough, right? Looks like components are even styled using CSS-like properties which is handy.
Something more complex
Of course, it wouldn’t do to learn about a GUI framework and not create a temperature converter. So let’s do so!
We’ll make a temperature converter which converts between degrees celcius and farenheight.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
This is mostly copy and pasted code from 7 GUI’s, but wow, there’s actually a lot going on in these 50 or so lines. The main thing to note here is the bidirectional binding between the class variables celsius and fahrenheit, when you update one textbox, the other updates too - this is going to be extremely useful!
So, where do we go from here?
There are a couple of applications I’d like to make to learn a bit more about Scala. I’d like to make an application like HarooPad which i’m using to crate new posts for this blog which is running Octopress; however I’d like the ability to parse the YAML in the header and for the GUI to be more minimal - all I want is a pane on the left for markdown and a live preview on the right.
I also play a fair amount of League of Legends, Riot Games has an official API to interact with game data and so I’d like to have a play around with this in Scala to make an application to see opponents stats or to analyse my performance in games.
I’m not sure which one would be easier to make first, or if I’d maybe have to do something smaller first. However, I feel I learn faster by having a project to complete and learning as I go - so many decisions…
That’s all for now!