Skip navigation

Monthly Archives: September 2010

There are often debates that run the risk of developing into flamewars about the relative merits of static and dynamic typing in popular programming languages. I feel I should air an issue that doesn’t get mentioned often. That is that there are a number of aspects of languages that are typically considered statically-typed that amount to dynamic typing.

In common object-oriented languages, there is frequently a mechanism for determining the type of an object at runtime. In Java, for instance, there is the “instanceof” operator. This can be combined with typecasting, autoboxing etc. to facilitate what amounts to dynamic typing. Unlike in, say, C, typecasting is type-safe: if an object does not conform to a particular type, then an exception will be raised. This is unequivocally dynamic typing. If one were feeling silly, one could type all method parameters and return types as “Object,” and, using these features, write a working, type-safe program. Many other statically-typed object-oriented programming languages contain similar features: Delphi had (or had, I’ve not touched it since version 7) “is” and “as”, and I believe C# has the same basic idea — which isn’t all that surprising, given the personalities behind that language; Go has type assertions that fulfil the same ends, and the “empty interface” offers a catch-all type, along the same lines as Java’s “Object” type.

Slightly more controversially, I would argue that the form of overloading typically referred to as “polymorphism” in these languages amounts to a form of dynamic typing. That is, the behaviour embodied in a particular method call (some of which, at least in theory, can be considered part of the type) depends not only on the interface that it refers to, but also on the run-time type of the object. This is something handled by the language, it doesn’t need to be an explicit concern of the programmer.

There are probably other examples that one could come up with. I think these demonstrate that there is value to dynamic typing beyond saving a few keystrokes on variable declarations, or what have you, and that even if one decides to use a static language to write a program, one can take advantage of this where appropriate. Unless it’s ML-derived or C or similar, as to my knowledge this sort of thing isn’t present in them.