Many people refer to me as a Java evangelist, Java super-freak, and many other terms that tend to suggest that any language other than Java is outside of the realm of consideration. Though true that I do my fair share of bashing of languages like Ruby or Groovy, it is because I really have longed for a new language to come out that is better than Java. I’ve been programming in Java for several years and though I love the language there are many flaws that don’t seem to be getting fixed any time soon. I know no language is perfect, but of the options I’ve always felt Java comes the closest.
When I first learned about Scala I shrugged it off as another crappy scripting language, but recently it has been brought back to my attention when I realized that it’s not a scripting language at all, but a fully language that gets compiled into byte-code the same way Java does. In fact, it gets compiled into byte-code that shares space with Java. For a Java developer this is quite appealing to be able to use a new language yet still make use of all my old APIs and conventions.
There are several amazing things about Scala that seem to pick up where Java falls short and runs with it. I will do my best to outline these features here as I’m learning the language to helpfully show these points to others and also for later reference for myself.
- Everything is an Object – This is particularly awesome in it gives true delegate support, something Java has been lacking forever. I can pass around methods as parameters and so much more.
- Native Support for XML – Though the ramifications of this feature can be some ugly code, when you want to work with XML in your applications being able to simply create a variable and reference direct XML without having to wrap it into a String it can be very helpful. This not only provides creation-time validation, but keeps you from having to escape quotes and nasty multi-line concatenation if you tried to do the same thing with a String. More useful than anything I think is the direct support to query data back out of XML.
- Overriding Operators – Reference back to point #1. Since everything is an Object even addition and subtraction is working with Objects. In Scala they have this cool idea to provide another syntax for making calls. For example, classically you would make a call to a method like:
However, in Scala you can also call the same thing like this:
myObject doSomething “somevalue”
My first thought when I saw this is more shock than awe. One of my biggest concerns as a developer is the open-ended structure in languages that allow programmers that are either new to programming or lack the common sense to write sensible code to exploit open-ended structure to do evil things to make their code completely unreadable. However, when I realized their purpose in doing this, the awe finally set in. Since a number is also an Object when I say:
3 + 5
That is the same as writing:
This means that + is actually a method that can be implemented so I can write pretty code to add two Objects together to a result.