Tuesday, April 08, 2008

Different perspectives on parameterized types

To the PL theorist, parameterized types are a necessity for the expressiveness of a statically typed language. Imagine Hindley-Milner without them: you'd have to duplicate definitions all over the place. But in practice, statically typed languages have loopholes--long before generics, Java always had the type Object so that you could essentially "turn off" type-checking when you couldn't type things precisely. This meant that you could always write generic operations and generic collections, but without type-checking.

So in practice, people don't look at parameterized types as increasing the expressiveness of Java at all; it just looks like a way to increase the amount of type-checking in the language, to make it stricter. And they're right.

So you have two correct perspectives on parameterized types: roughly, that they make the language less restrictive and more restrictive. It's not actually a contradiction, but it's enough to have caused me confusion in some conversations.

1 comment:

Kenn Knowles said...

The two perspectives you present seem to view types as 'filters' to catch the bad programs. In my programming practice, this "error catching" is just a side-effect of "structure describing". I can't imagine life without parametric polymorphism because there would be so many ideas I couldn't write down (in types). Hindley-Milner polymorphism is a bare minimum!

I guess before I think in my "statically typed language" I think in my "type language". And in the latter, parameterized types are strictly more expressive than loopholes.