The Scala Type System: Is it too complicated for Java programmers?
By Cameron McKenzie
Is the type system in Scala too complicated?
It's a common complaint that newcomers to Martin Odersky's JVM language tend to voice, but is it a fair complaint, especially when compared to what a Java programmer is used to? Not according to Josh Suereth, the author of the highly acclaimed Scala In Depth, it isn't.
"Java's type system is good for really basic stuff, but once you hit Scala, you realize that you can write pretty expressive code, while at the same time, keep things pretty simple," says Josh (@jsuereth), a Senior Software developer for Typesafe, and an expert who is passionate about both Scala and Java. "Having gone from Scala and back to Java, and trying to enforce anything in the type system, it can be a very frustrating endeavor."
Comparing Type Systems
It is a bit disingenuous, however, when Java aficionados start complaining about the complexity of another language's type system. After all, it wasn't until Java 5 that generics were added to the language to address the lack of type control in the Collections API, which is arguable the most important part of the language. And what's more, generics in Java can quickly turn into an unwieldily mess of nested less than and greater than signs as types get embedded within other types in a very hall of mirrors type fashion.
But for now, the microscope is on Scala, not Java. So if the type system isn't all that overwhelming, why do so many Scala neophytes have such an issue with it?
The right tool for the job
"One of the common problems that people new to Scala have is that they try to do all of this crazy stuff in the type system when they don't need to," says Josh. Be it actors or implicits or the powerful type system, Scala puts a number of new tools into the enterprise developer’s toolkit. But becoming adept at Scala, as with anything else, means knowing when a given tool is right for the job. "All of a sudden you have a new power that you didn't have before and you use it all over your code. Understanding when to preserve types with the type system and when not to use it is a big thing."
Of course, zealous new Scala developers can't be held completely responsible for an over-enthusiastic use of a type system that they might not completely understand. Being a relative newcomer to the scene, Scala isn't nearly as well documented as other, more mature programming languages, and many of the brightest minds on the scene are too busy doing actual development to write about best practices, design patterns and advanced usage strategies, which is what Josh is hoping to address with Scala in Depth. "The motivation behind writing Scala in Depth was basically that it was needed. There are a whole bunch of introductory Scala books, but there was nothing for intermediate to advanced developers." And that's the audience that Josh is targeting with this release, the intermediate to advanced developers who are looking at taking their knowledge of the Scala programming language to the next level.
But back to the original question: is the type system
in Scala too complicated? On that topic we'll give Josh the final word. "Yes, it's probably more
complicated than what you're used to. But it's only as complicated as you need it to be for the
problem that you are solving."
08 Jan 2013