|
|
|
|
|
|
STRONGLY-TYPED PROGRAMMING LANGUAGE
In computer science and computer programming, the term strong typing is used to describe how programming languages handle datatypes. The antonym is weak typing. However, these terms have been given such a wide variety of meanings over the short history of computing that it is often difficult to know what an individual writer means by using them.
Programming language expert Benjamin C. Pierce, author of Types and Programming Languages and Advanced Types and Programming Languages, has said:
- "I spent a few weeks... trying to sort out the terminology of "strongly typed," "statically typed," "safe," etc., and found it amazingly difficult.... The usage of these terms is so various as to render them almost useless." [verification needed]
Most generally, "strong typing" implies that the programming language or implementation enforces some kind of constraints upon programs, preventing them from running code which uses data in an invalid way. For instance, an integer addition operation may not be used upon strings; a procedure which operates upon linked lists may not be used upon numbers. However, the nature and strength of these constraints is highly variable.
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 (usually when they are declared) rather than values (usually when they are created).
- The mandatory requirement, by a language definition, of compile-time checks for type constraint violations. That is, the compiler is required by the check that operations only occur between certain kinds of operand types.
- 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. Values of one type cannot be converted to another type, either explicitly or implicitly.
- Some authors, however, reserve the phrase "strongly-typed language" for languages that omit implicit type conversion (that is, conversions that are inserted by the compiler on the programmer's behalf). For these authors, a programming language is strongly typed if types must be converted by an explicit notation, often called a cast.
- The absence of ways to evade the type system. Such evasions are possible in languages that allow programmers to get at the underlying representation of values (ie, their bit-pattern).
- 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:
- Both Pascal and C require all variables to have a defined type and support the use of explicit casts of arithmetic values to other arithmetic types. Pascal is often said to be more strongly typed than C, a claim that is probably based on the fact that C supports more kinds of implicit conversions than Pascal and C also allows pointer values to be explicitly cast while Pascal does not.
- 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 list assigned to a scalar variable is quietly turned into the length of the list, which may conceal an error.
- Common Lisp has a complex, fine-grained system of data types, but is almost entirely dynamically typed.
- Visual BASIC is a hybrid language. In addition to including statically typed variables, it includes a "Variant" data type that can store data of any type. Its implicit casts are fairly liberal where, for example, one can sum string variants and pass the result into an integer literal.
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.
|
|
|
|
|
|
|