Showing posts with label static typing. Show all posts
Showing posts with label static typing. Show all posts

Monday, February 11, 2008

Concretizing Static Typing Metadata

Well, that's a pretentious title, doncha think?

Steve Yegge writes in Portrait of a Noob that static typing is effectively meta-data ("we also know that static types are just metadata"), like comments, and so isn't strictly required for the compilation and execution of software. He's right in a limited context. If static typing is being used for nothing more than ensuring "type matching", and really doesn't add anything beyond that, then it is effectively just a stronger form of commenting, with the compiler acting in the role of object compatibility inspector.

This does get at why the argument for "type safety" has never achieved much success as a compelling reason for using a strongly typed language. Merely making sure your objects are compatible is a good thing, but it does constrain flexibility, extensibility, and adds type management overhead (to the programmer).

If strong typing is going to be seriously valuable it has to do more than merely ensure type safety, it needs to actually add concrete information to the software.

Take a programming language like Ada, considered one of the paragons of strongly typed programming languages. It does all the type safety stuff, and Ada advocates are more than happy to promote that as one of its great virtues for creating and delivering reliable, safety-critical software. All true, but obviously type safety, accompanied by its supporting syntax and semantics, was not sufficiently compelling to drive any significant adoption outside the defense and aerospace industries (and in those fields, of course, much of the initial impetus was mandate-driven anyway).

What most of the Ada programming language advocates overlooked was the productivity gain possible by the language's specific implementation of strong typing. When its advocates talked about strong typing aiding productivity, it was nearly always in terms of error avoidance. Again, true, and a good thing, but hardly sexy. After all, how many programmers are going to willingly admit that they write buggy code and that maybe they should look into using a programming language that would help them avoid errors?

I went into some detail about this in The Fundamental Theory of Ada, describing how the specifics of Ada's "type model" allows the Ada programmer to implicitly embed scads of additional information with no effort beyond that of defining a type. The language specifies all the additional programmatic information directly accessible to the programmer pertaining to that type. In a sense, user-defined type definitions implicitly declare an associated class instance with information relevant to that type. Here's an excerpt from Ada:

type Speed_Range is range 0 .. 1000;

With nothing more than a reference to an object of that type:

Speed : Speed_Range;

One can know its minimum value (Speed_Range'First), maximum value (Speed_Range'Last), the minimum number of bits needed to represent all possible values of the type (Speed_Range'Size), the actual number of bits representing a variable of that type (Speed'Size, which is often larger than the type size since objects almost always occupy a whole number of bytes), the number of characters needed to represent the longest possible string representation of values of that type (Speed_Range'Width), etc. You can convert values to and from strings (Speed_Range'Image, Speed_Range'Value), do min/max comparisons (Speed_Range'Min(100, Speed), Speed_Range'Max(Current_Max, Speed)), and use the type as a loop controller ("for S in Speed_Range loop" and "while S in Speed_Range loop"), and more. And none of this information needs to be explicitly programmed by a developer, it is all implicitly provided by the mere definition of the type.
This is where strong typing is far more than disposable metadata, like comments. This "aggressive" approach to strong typing, whether in Ada or a similarly conceived programming language, "concretizes" the metadata into practical use to not merely aid error avoidance, but to actively increase programmer productivity.