Donnerstag, 16. April 2009
Java type system anomally
In the last days I was hacking a small system integration application in Scala. It was really a pleasure to work with a reasonable type system. During that I remembered several of the “well known” Java Type anomalies especially following code fragment:

Integer ints[]=new Integer[1];
Object objs[]=ints;
objs[0]="Hallo Arno";

A really nice one, which I really like. It compiles without any warnings, but it is completely broken. For inexperienced ( Java) developers maybe a little hard to spot why. It would be easy and nice to create a nice Java Puzzle out of this theme. The java-generics documentation states,

“Because the program compiles without warnings, we can state with certainty that it will not throw a ClassCastException at run time.”

This is right, because the code throws a java.lang.ArrayStoreException. So the statement is formally correct, but in practice, there is no difference. In any case the method terminates with a runtime exception. On the other hand I understand the initial design forces for the java team. They wanted a simple typed language and they wanted to be able to write some array handling library code.

I haven´t made up my mind jet, which solution to this dilemma I would prefer. These are:

  1. The highly sophisticated inheritance model of the latest languages like scala
  2. the dynamic duc typing of languages with a pure dynamic type system like J/Python, J/Ruby,
But for one think I am sure, I prefer both solutions over the pure old fashioned Java type system. What do you think?

... comment

dynamic vs. static
From my own experience the dynamic typing favors rapid prototyping. You get to the first results very fast. One doesn't have to design all the intrinsic details of typing. Just "code ahead".
But it takes a whole lot of discipline and focus to get things right. And I am not quite sure if it doesn't take longer until a reasonable consistent and bug-free version has been build.

So I'm more inclined to the static typed world. While I literally "hate my compiler" while it spams me with its complaints I know that it's a cleaning process which helps me on my way to craft a reasonable good piece of software.

With that in mind I really like scala. The compiler is smart enough to relief me from unnecessary "boiler plate" and scala has some advanced features (apart from the strong type system) which come in handy.

But it has it's gotchas also. I did a lot of coding in perl. And I constantly felt the temptation to leave the path of "readable code" because of the options to do so. But terse code is much more difficult to understand (and it's a commonplace that code is more often read than written).
Scala gives me to some extend the same feeling. I fear that it's syntactic power can be easily misused leading to obfuscated code.

... link  

... comment