Bryn Keller
Posts: 3
Nickname: xoltar
Registered: Jul, 2003
|
|
Re: Strong versus Weak Typing
|
Posted: Apr 13, 2004 10:41 AM
|
|
> One thing I've noticed is usually the proponents of > weak (I mean run-time) type checking have tried developing > with both types of languages and have made a choice while > the proponents of strong (I mean compile time) have only > used languages with no checking (like C) with bad results, > switched to compile time checking (like C++ or Java) and > had better results and have never actually developed with > run-time checking. They are confused about what is really > going on and imagine disastrous things happening. In fact, > the disasters don't actually happen. I have my own > theories about why not but have no way of proving or > disproving them.
Well, I'd have to disagree with that generalization. I started with Java, fully embraced Python for several years, and now that I've had a chance to think about it, I'm on the static typing side - that doesn't mean I'm back Java, though! I was always irritated at Java's type system - the glaring holes (casts, e.g.), and the excessive finger typing. I switched to Python and never looked back. Free at last!
But then I realized something: Java and C++ are not the last word in static typing. Modern static typing involves very little finger typing (sometimes none), while providing stronger static checking than C++ or Java provide. There's nothing saying that static typing has to mean code like this:
SomeLongName foo = new SomeLongName();
The same thing in Nice ( http://nice.sourceforge.net ) would look like this:
let foo = new SomeLongName();
or maybe you'd like this:
String[] strings = new String[] {"one", "two", "three"};
reduced to this:
let strings = ["one", "two", "three"];
Doesn't look much different from Python, right? But this is statically typed code.
The compiler can very well figure out for itself what the types of foo and strings should be, it shouldn't need me to tell it explicitly. I think we should more from our compilers, rather than giving up on static typing out of disgust with C++ and Java.
> While using Smalltalk I did have some type errors which > were caught at runtime but not not nearly as many as you > would think if you counted the number of compile time type > errors I get in Java and assumed they would be equal.
Yet many new Haskell users are shocked and amazed to find that in many cases, if the compiler will accept their code, it runs correctly on the first try. These people don't receive runtime type errors at all. They don't have to write tests to catch them, they just don't have to worry about a whole class of errors. This is a much stronger guarantee than you are probably used to from Java/C++.
I'm not proposing that static typing is a replacement for testing, but it's a valuable tool that we shouldn't be so quick to throw away. See my article "Modern Static Typing: Less Code, Better Code" at http://www.xoltar.org/misc/static_typing_eckel.html for a more detailed statement.
> Also, I > honestly can't remember even once that a bug that was > discovered in testing turned out to be a wrong type that > wasn't caught by the run-time type checking.
Sure, we're not talking about bugs that can't be caught by run-time checking, we're talking about not waiting until runtime to catch them.
> My guess is that it was because I was > able to concentrate more on what I was doing as opposed to > keeping a compiler happy. Most of my type mistakes caught > by the Java compiler I think are less because I had an > unclear notion of what type I was passing and more because > I make some silly mistake in tediously explaining and > reassuring the compiler that I know what I'm doing.
Fair enough. You need a smarter compiler! You might try Nice http://nice.sourceforge.net , Haskell http://www.haskell.org , or Ocaml http://caml.inria.fr . There are many others also, but these are the ones I'm most familiar with.
|
|