So lately I’ve been working on my LibGDX tutorial series and I am certainly a fan of the library. Java on the other hand, after years of using C#, just seems flawed. Not that it is a bad language, just that it’s a bit kludgy. Fortunately there are a number of languages built over top of the Java Virtual Machine, allowing you to make use of most of the Java eco-system, while working in a different language. Some of the more popular options are Groovy, a scripting language that targets the JVM and Clojure, a functional LISP like language. I don’t personally want a fully dynamic language ( I like typed languages for large projects ) so Groovy is out, while LISP might as well be Klingon. I like some functional programming, but my brain just doesn’t work that way… to warped by years of procedural programming.
Fortunately there is Scala.
I’ve only just started playing with it but I am already impressed. It’s almost as if someone took all the aspects of Java I dont like and set out to fix them. Things I like:
- runs on the Java VM, so can use libraries like LibGDX without issue but still feels familiar
- type inference. Feel like a dynamic language while staying dynamically typed. I miss var from C#!
- Functional programming lite. High Order functions.
- It’s got REPL ( command line programmability ) even if it’s faked. Great way to learn the language.
- Everything is an object, one of Java’s biggest warts
- Pattern matching… it’s like an uber switch statement and looks to be a huge time saver
- traits and sealed… I think. Basically a trait is an interface with codability, while sealed allows a class to define which classes can extend it. It will take some use, but both seem to solve commonly encountered problems, but both may have huge downsides.
- makes the language much more compact while still feeling like Java.
- operator overloading. This was simply a stupid Java mistake.
- best conditional expression evaluation I have ever seen. Optional semi colons.
I’m still just at the beginner phase, but I have to say I’ve already had a ton of AHAH moments. There are a few annoyances, at least initially. For example, I dont like the variable coming after the variable name… there might be a huge win here somewhere, but it feels very unnatural coming from Java.
Anyway, back when I started looking to catch up on Java I looked for a book that taught Java but did so making certain assumptions about the programmers experiences. For example, I know what a class is, how a loop works, etc… Sadly I never found such a book. This time however, for learning Scala, I did.
This book is exactly what I was looking for when I was looking for a Java book for experienced programmers. In the authors own words:
I wrote this book for impatient readers who want to start programming in Scala right away. I assume you know Java, C#, or C++, and I don’t bore you with explaining variables, loops, or classes. I don’t exhaustively list all the features of the language, I don’t lecture you about the superiority of one paradigm over another, and I don’t make you suffer through long and contrived examples. Instead, you will get the information that you need in compact chunks that you can read and review as needed.
From what I have read this is exactly true. I have no prior Scala experience, but I have never found myself once struggling with any concepts presented in this book. Nor frankly have I been bored, something I often struggle with for programming books.
I need to make something extremely clear. If you do not have a solid prior programming foundation in C# or Java ( or possibly C++ ), this is not the book for you! The book basically covers how Scala deviates from other languages, so if you don’t know the fundamentals, you will struggle. It is also not a language reference. If you are new to programming or want a language reference, Programming in Scala is probably the book you want.
One other thing I really appreciate about this book is the authors writing style. It’s an easy read and he has a sense of humour. Here for example is his tip on operating overloading:
In Java, you cannot overload operators, and the Java designers claimed this is a good thing because it stops you from inventing crazy operators like [email protected]$&* that would make your program impossible to read. Of course, that’s silly; you can make your programs just as hard to read by using crazy method names like qxywz. Scala allows you to define operators, leaving it up to you to use this feature with restraint and good taste.
That paragraph is pretty typical of how the book goes.
So I’m heading off on this Scala adventure in my spare time. Expect a few related posts here and there as I go.