Sunday, February 9, 2003 [Tweets] [Favorites]

Types

Artima has posted an interview with Guido van Rossum who, not surprisingly, likes Python’s type system. Unfortunately, from what I’ve seen there’s little agreement about what “strong” and “weak” typing actually mean. There are several dimensions along which a type system can vary, including:

Java and C++ both require type declarations. Both do a lot of type checking at compile-time, but Java does a lot of runtime type checking that C++ does not. A bad cast in Java will give you a ClassCastException, instead of a crash or an incorrect result. One might say that Java and C++ are both strongly typed, but that Java is safe whereas C++ is unsafe. Objective-C has the id type, which mostly makes types not matter at compile time. At runtime, it gives you exceptions for certain kinds of errors (e.g. “selector not recognized”) but not others. Python is kind of like Java without type declarations or compile-time checking—although it can do some compile-time checking via PyChecker.

You’ll never get all the bugs out. Making the code easier to read and write, and more transparent to the team of human readers who will review the source code, may be much more valuable than the narrow-focused type checking that some other compiler offers.

This is true, but types can also be a useful form of documentation for yourself, or whoever looks at the code next. It’s about balance, I think. One of my favorite approaches is the one Dylan uses: safety with optional type declarations that can be added as you refine the code to make it faster and more robust. I’m kind of surprised that Guido didn’t mention type inference, as in many ways it lets you have your cake and eat it too.

Comments

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment