Strong typing

from Wikipedia, the free encyclopedia

Strong typing , also strict typing , is a principle in the typing of programming languages . Definitions of varying rigor can be found in the specialist literature. In general, however, one can differentiate between languages ​​with no or very weak typing (e.g. PHP ) and languages ​​with stronger typing ( e.g. C ++ ). Often it is strictly related to static typing and expanding it.

In the case of dynamic typing within the framework of reference variables , inheritance or polymorphism - the latter can often only be checked when the corresponding programs are running - there are very large differences in the strength of the typing. Programming languages, which are only very weakly typed in dynamic variables such as C, allow the assignment of addresses to any reference variables. Very rigorously designed programming languages ​​only allow strictly compatible assignments , but for practical reasons many programming languages ​​tolerate implicit type conversions with no or negligible loss of information.

Strong typing is mostly used as a comparison for more than absolute name: Language X is stronger / more stringent typed as language Y . In such comparisons, the presence of implicit or explicit conversions very often plays a role; PHP is typed weaker than C because PHP implicitly converts almost everywhere, but C only for similar types. In turn, C ++ is typed more strongly than C, since z. B. implicit conversions of void * into any other pointers are not allowed. Object Pascal is even more strongly typed , which in contrast to C ++ does not perform any implicit conversions between Boolean and integer values. And Haskell and Rust don't even allow implicit conversions from integers to floating point numbers.

Different definitions

(descending according to the severity of the respective definition)

  • There are no methods for converting a variable to another type. The type system cannot be bypassed.
  • Only explicit conversions are possible.
  • Types are checked at compilation time ( static typing ). This is in contrast to a check at runtime ( dynamic typing ).
  • Implicit conversion is only possible between similar types (see also assignment compatibility )
  • General distinction between types (e.g. Tcl does not have different types)

Advantages and disadvantages

Advantages of strict typing are for example

  • Optimization possibilities
  • Rejection of faulty code at compilation time (particularly important for highly critical areas of application, e.g. aircraft or satellite control)
  • Early detection of programming errors with the reduction of the effort for troubleshooting with a corresponding gain in time and efficiency during implementation
  • A performance gain can result from static type conversion compared to dynamic type conversion

Disadvantages include:

  • Multiple use of code is partially restricted (separate functions have to be written for partially very similar types), but this can not only be avoided by inheritance of program code or polymorphism, for example in the context of generic programming , but can even be solved in a particularly elegant and well-structured manner .
  • For languages ​​without type inference, more paperwork and possibly poorer reading flow, thus configuration via convention.

Realization of strict typing in different programming languages

  • C has a rather weak typing, since it allows an implicit or explicit conversion in many situations, especially in connection with pointers . In addition, C does not distinguish between the data types of truth values, letters and small numbers.
  • Compared to C ++ , C ++ has an extended type system (due to the object orientation you can define your own types), in which implicit and explicit conversions, especially between newly defined types (classes), are more strictly regulated; for example, object pointers may only be implicitly converted into a pointer to an object of the base class if the inheritance is public . In addition, in contrast to C, void pointers must not be converted into pointers to any objects; an explicit conversion must be carried out. In C, an implicit conversion is performed.
  • In C, C ++, Java , C # , Pascal , Ada or Visual Basic .NET (with Option Explicit On) all variables must have a type.
  • Perl has a relatively weak typing, so numbers and strings share a data type that is context-dependent, depending on the operator used, used as a number or string. Only if a strict typing is required is a warning issued in the event of any undesired conversions.
  • Ruby is strongly typed. Every object has a fixed class and explicit methods (to_s, to_i etc.) must always be called when converting. Conversion is only implicit in the Boolean context (nil to false, everything else to true).
  • OCAML , Haskell and Rust do not allow implicit type conversions.
  • Since version 7 PHP has allowed strict typing to be activated within individual files.
  • Visual Basic Classic and Gambas have both statically typed variables and the Variant type, which can contain any type.
  • Assembly language and Forth have no typing; only the programmer can / must do the type check.
  • C # , Modula-2 and Oberon have strict type systems in which the assignment compatibility of both static types and dynamic types is checked strictly during compilation. The assignment compatibility of a variable to a different data type in the source program can only be explicitly enforced in special cases. Oberon provides explicit type guard for instances of dynamic data types in the source program , which however do not carry out any type conversion, but only monitor the assignment compatibility at runtime.

Individual evidence

  1. Christoph-Probst.com: Informal method and compiler techniques for uncovering programming errors ( Memento from January 20, 2010 in the Internet Archive ) (PDF)
  2. Data Types - The Rust Programming Language. Retrieved November 2, 2018 .
  3. PHP: Function parameters, sub-item “Strict typing” - Manual. Retrieved December 20, 2017 .
  4. Category: Forth - Rosetta Code. Retrieved November 2, 2018 .
  5. Daniel Larson: Introduction to strongly Typed Data Sets ( Memento of the original from March 25, 2016 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. , January 25, 2003, accessed March 22, 2016 @1@ 2Template: Webachiv / IABot / www.csharp-station.com
  6. ETH Oberon White Paper , ETH Zurich, March 13, 2000, accessed on March 22, 2016
  7. Massimo Felici, Karama Kanoun, Alberto Pasquini: Computer Safety, Reliability and Security , 18th International Conference, SAFECOMP'99, Toulouse, France, September 27-29, 1999, Proceedings Lecture Notes in Computer Science, page 195, Springer, 2003 , ISBN 9783540482499
  8. ^ Niklaus Wirth : The Programming Language Oberon , ETH Zurich, October 1, 1990, accessed on March 22, 2016