Strongly-typed programming language
From Free net encyclopedia
The specification of nearly every high level computer programming language includes the concept of type. Variables are declared to have a type and a language's specification will include rules for the behavior that occurs when variables having different types occur together (eg, as operands in an expression). Some languages (eg, PL/I) go to great lengths to allow variables of almost any type to be intermixed, while other languages (eg, Ada) severely restrict the mixing of variables having different types.
Two examples:
-
3 + "4"
(the operands of the plus operator are likely to have types integer and string respectively). Some language specifications regard the types integer and string as being non-compatible in this context and require that a compiler flag this code as non-conforming. Other languages assume that the programmer intends the string to be converted to an integer value and require that the compiler insert the necessary conversion code. Still other languages assume that the value 3 should be converted to a string and the plus operator interpreted as concatenation rather than addition, resuting the in string value "34". -
5 + 6.2
(the operands of the plus operator are likely to have types integer and real respectively). Most languages regard the types integer and real to be sufficiently close to each other that they support this usage (with code to convert the integer to a real type being inserted by the compiler). A few languages would not regard these two types as being compatible in this context.
A language that places many restrictions on how variables of different types may interact with each other is said to be strongly typed, while a language that has few such restrictions is said to be weakly typed. Whether a given language is viewed as being strongly or weakly typed often depends on the person making the judgement. Being strongly typed is often seen as a positive attribute of a programming language and supporters of a language may refer to it as being strongly typed for this reason.
Some of the things which writers have described as "strong typing" include:
- Static typing as opposed to dynamic typing. In a static type system, type annotations are associated with variable names rather than values. It is thus possible for the compiler to prove via static analysis that a program contains no type errors.
- The mandatory presence of compile-time checks for type constraint violations. That is, the compiler actually does perform static analysis, and rejects programs which fail it.
- Type safety; that is, the rejection (at either compile or run time) of operations or function calls which attempt to disregard data types. In a more rigorous setting, type safety is proved about a formal language by proving progress and preservation.
- The disallowing of type conversion. Variables or values of one type may not be used in the place of those of a related type. Integers and floating point numbers, for instance, may be held strictly separate.
- The disallowing of implicit type conversion. Types must be converted by an explicit notation such as a function that accepts one type and returns a different one.
- The absence of ways to evade the type system. C-style casts, or the ability to use a pointer to one type as if it had contained a different type, are disallowed.
- A complex, fine-grained type system with compound types.
- The assignment of fixed and invariable type to data objects. The type of a given data object does not vary over that object's lifetime. Class instances, for example, may not have their class altered.
- Strong guarantees about the run-time behavior of a program before program execution, whether provided by static analysis or another mechanism.
Note that some of these definitions are contradictory, while others are merely orthogonal. Because of the wide divergence among these definitions, it is possible to defend claims about most programming languages that they are either strongly- or weakly-typed. For instance:
- C compilers perform static analysis about many - but not all - classes of variables, but permit the programmer to easily override the type system using casts.
- Perl auto-assigns a type to every variable which completely avoids the type mismatch between variable declaration and variable use, but performs implicit conversions readily, for instance a number used in a string context is quietly converted to a string, which may conceal an error.
- Common Lisp has a complex, fine-grained system of data types, but is almost entirely dynamically typed.
For this reason, writers who wish to write unambiguously about type systems often eschew the term "strong typing" in favor of specific expressions such as "static typing" or "type safety".
For a more thorough discussion of typing issues, see the article on datatypes and its related topics.de:Starke Typisierung es:Lenguaje de programación fuertemente tipado fr:Typage fort it:Forte tipizzazione pl:Silna typizacja