Strong and weak typing

Strong Typing (English strong typing, German often strong typing ) refers to a principle in the typing of programming languages, however, this is not clearly defined. In the literature there are different definitions strict. But you can generally between languages ​​with no or very weak typing ( eg PHP ) and those with stronger typing (among C ) differ. Often hangs together with strict static typing and extends it.

With dynamic typing in the context of reference variables, inheritance or polymorphism - the latter can often only be checked at run time the corresponding programs - there are very large differences in the strength of typing. Programming languages ​​that are typed only very weakly with dynamic variables, such as C, allow the assignment of any addresses to reference variables. Very rigorously designed programming languages ​​allow only strictly incompatible assignments, many programming languages ​​tolerate However, for practical reasons implicit casts without or with negligible loss of information.

Strong typing is used mostly as a comparison because as an absolute name: Language X is stronger / stricter typed as language Y. In such comparisons very often the existence of implicit or explicit conversions also plays a role; so PHP is therefore weaker typed as C because PHP almost everywhere implicitly converted to C but only for similar types. C in turn is therefore stronger typed than C, since, for example, implicit conversions from void * are not allowed in any other pointer. Even more typed in turn Object Pascal, in contrast to C does not perform implicit conversions between Boolean and integer values. And Haskell is not even allowed implicit conversions of all- in floating point numbers.

Various definitions

(in descending order according to severity of each definition)

  • Absence of various conversion methods and no way to circumvent the type system in any way
  • Only explicit conversions possible
  • Type checking at translation rather than at run time ( vs. static typing. Dynamic typing )
  • Implicit conversions only between similar types ( for example, two different types of integer, see also assignment compatibility )
  • General distinction between types (eg, Tcl has no different types)

Pros and Cons

Advantages of strongly typed, for example

  • Performance gain by avoiding the time-consuming type conversions
  • Optimization options
  • Rejection of bad code already at compile time (especially important for highly critical application areas, such as aircraft or satellite control )
  • Early detection of programming errors with the reduction of the effort for troubleshooting with appropriate time and increased efficiency in the implementation

Disadvantages include:

  • Multiple-use of code is partially restricted ( it must own functions for partially very similar be written ), but which is not only avoided by inheritance from program code or by polymorphism, for example in the context of generic programming, but can even be solved in a particularly elegant and well structured.

Realization of strongly typed in different programming languages

  • C has a rather weak typing, because it is an implicit or explicit conversion allowed in many situations, especially in the context of pointers. Moreover, C does not distinguish the data types of truth values ​​, letters, and small numbers.
  • C is a C towards extended type system (due to the object orientation can define your own types ), in this are implicit and explicit conversions, particularly between newly defined types (classes ) strictly regulated; for example, may be implicitly converted to an object of the base class to a pointer object pointer only if the inheritance is public. In addition, may, in contrast to C void pointer can not be converted to any item in hand, it must be made an explicit cast. In C, an implicit conversion is performed.
  • In C, C , Java, C #, Pascal, Ada or VB.NET ( with Option Explicit On) all variables must have a type.
  • Perl has very limited strong typing: Only when desired strongly typed a warning is issued for any non- desired transformations.
  • Although Python has a relatively strong typing, but this is not visible to the programmer (variables are typeless references to objects ); Duck typing is used.
  • OCaml and Haskell do not allow implicit type conversions.
  • Visual Basic Classic has both statically typed variables as well as the variant type, which must not contain any types.
  • Assembly language and Forth have no typing; Only the programmer must / type checking must be done.