Jump to content

Strong and weak typing

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 166.182.83.127 (talk) at 22:04, 11 September 2015. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Abstract weak

In computer programming, programming languages are often colloquially classified as strongly typed or weakly typed. These terms do not have a precise definition, but in general, a strongly typed language is more likely to generate an error or refuse to compile if the argument passed to a function does not closely match the expected type. On the other hand, a very weakly typed language may produce unpredictable results or may perform implicit type conversion.[1] A different but related concept is latent typing.

Ban

Definitions of "strong" or "weak"

A number of different language design decisions have been referred to as evidence of "strong" or "weak" typing. In fact, many of these are more accurately understood as the presence or absence of type safety, memory safety, static type-checking, or dynamic type-checking.

Ban

Abstract=== tagged unions === Some programming languages support untagged unions, which allow a value of one type to be viewed as if it were a value of another type.

Ban

Ban

Abstract === Type inference === Languages with static type systems differ in the extent to which users are required to manually state the types used in their program. Some languages, such as C, require that every variable be declared with a type. Other languages, such as Haskell, use the Hindley-Milner method to infer all types based on a global analysis. Other languages, such as C# and C++, lie somewhere in between; some types can be inferred based on local information, while others must be specified. Some programmers use the term weakly typed to refer to languages with type inference, often without realizing that the type information is present but implicit.

Abstract == Variation across programming languages == Note that some of these definitions are contradictory, others are merely orthogonal, and still others are special cases (with additional constraints) of other, more "liberal" (less strong) definitions. 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:

  • Java, Pascal, Ada and C require all variables to have a declared type, and support the use of explicit casts of arithmetic values to other arithmetic types. Java, C#, Ada and Pascal are sometimes 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, and C also allows pointer values to be explicitly cast while Java and Pascal do not. Java itself may be considered more strongly typed than Pascal as manners of evading the static type system in Java are controlled by the Java Virtual Machine's type system. C# is similar to Java in that respect, though it allows disabling dynamic type checking by explicitly putting code segments in an "unsafe context". Pascal's type system has been described as "too strong", because the size of an array or string is part of its type, making some programming tasks very difficult.[2][3]
  • Smalltalk, Perl, JavaScript, Ruby, Python, and Self are all "strongly typed" in the sense that typing errors are prevented at runtime and they do little implicit type conversion, but these languages make no use of static type checking: the compiler does not check or enforce type constraint rules. The term duck typing is now used to describe the dynamic typing paradigm used by the languages in this group.
  • The Lisp family of languages are all "strongly typed" in the sense that typing errors are prevented at runtime. Some Lisp dialects like Common Lisp or Clojure do support various forms of type declarations[4] and some compilers (CMUCL[5] and related) use these declarations together with type inference to enable various optimizations and also limited forms of compile time type checks.
  • Standard ML, F#, OCaml and Haskell are statically type-checked but the compiler automatically infers a precise type for all values.
  • Visual Basic is a hybrid language. In addition to variables with declared types, it is also possible to declare a variable of "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 variable.
  • Assembly language and Forth have been said to be untyped. There is no type checking; it is up to the programmer to ensure that data given to functions is of the appropriate type. Any type conversion required is explicit.

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 "type safety".

See also

References

  1. ^ http://www.cs.cornell.edu/courses/CS1130/2012sp/1130selfpaced/module1/module1part4/strongtyping.html
  2. ^ "InfoWorld". Retrieved 16 August 2015.
  3. ^ Brian Kernighan: Why Pascal is not my favourite language
  4. ^ "CLHS: Chapter 4". Retrieved 16 August 2015.
  5. ^ "CMUCL User's Manual: The Compiler". Retrieved 16 August 2015.