One of my pet peeves is folks who claim this or that programming language is "strongly typed" when they really mean it is "statically typed." In a statically typed programming language, the types of variables and expressions is determined prior to run time, i.e., language processing time a.k.a. "compile time." This is in contrast to "dynamically typed" languages where the types are not known until run time. In such languages, the same variable might even have different types at different points of run time. In a dynamically typed system applying the "+" operator to a number and a string won't cause problems at language processing time, but at run time three things might happen: an error, a conversion of the number to a string resulting in a concatenation, or an attempt to convert the string to a number resulting in numerical addition.
Strongly typed languages never have run-time type errors. This is a lot stricter than simply static type checking. This also means at least well-defined behavior for things like an index out of bounds issue on an array or dereferencing an invalid pointer such as a "null." The ML language and some of its derivatives achieve strong typing by simply not supporting arrays or pointers. C is statically typed, but can't be considered strongly typed because it may silently error at run-time with these kinds of errors. Java throws exceptions for these kinds of errors which is well defined behavior and can be considered distinct from an error. I'm not convinced Java is strongly typed and throwing exceptions seems like a less desirable thing than simply preventing such problems at language processing time.
Strong typing also implies that even explicit type casts can't have undefined behavior and certainly means that implicit type casts won't be attempted if information might be lost. So, if your language implicitly converts from a 32-bit integer to a 32-bit IEEE floating point value, it probably isn't strongly typed. If you can downcast in an OO hierarchy your language might not be strongly typed either.
Yet another confusion with static typing is the notion that static typing requires explicitly declaring types of variables. Again, ML is the classical counterexample where type inference is used rather than explicit type declaration. Newer language systems such as C++11, D, and Swift all use type inference to at least reduce the amount of explicit type declaration that needs to be done. Type inference requires more sophisticated language processors and increases the amount of time needed to compile a program. Hence, while the technology to do type inference is fairly old, many language designers avoided this feature until Moore's law made it more practical. In type inference, if one sees an expression like "i+1" the type of i can be inferred to be the same as the type of 1, probably an integer. When using complicated types whose declarations require a lot of typing and are often non-obvious, type inference makes the programmer's job much easier.
No comments:
Post a Comment