Type system

A typing is used in computer science, that the objects ( here objects understood in mathematical and abstract sense ) are the programming languages ​​, such as variables, functions or objects used correctly (in the sense of object-oriented programming). One goal of typing is to avoid runtime errors.

Type System

The term type system referred to in the computer science a component that can be used in programming languages ​​to restrict the range of values ​​of variables. Programming languages ​​that have a type system, called typed. Depending on the nature of the type system is called occasionally also of strongly typed or weakly typed languages. By typing is to ensure that the contents of variables no operations are performed that are syntactically or semantically incorrect. A type system is formed by the following components:

  • The types themselves, which are either generated by type definitions or firmly integrated in the language ( as primitive data types).
  • The ability to program elements to declare ( such as variables, method parameters, etc. ) by means of Typannotation with a particular type.
  • Rules that are associated with a particular type, the values ​​of expressions.
  • Rules for checking the type correctness of assignments.
  • Optional additional language components such as type-based operators (such as " instanceof " in various languages ​​), or a reflection API to define and test of type information at run time.

Classification of type systems

Type systems can be of three dimensions along classify:

  • Strong typing (strong typing ) and weak typing ( weak typing )
  • Dynamic typing (dynamic typing ), and static typing ( static typing )
  • Explicit typing (explicit typing ) and implicit typing ( implicit typing )
  • Optional typing ( optional typing ) type annotations are used to support the programmer, but have no effect on the actual implementation by the compiler at runtime.

One of the tasks of a type system include:

  • Detection of type injuries in the translation and execution. The type information can be seen as a redundant information, which prevents the variables are assigned values ​​, which should never assume the variable in question (so-called Typinvarianten ). So prevents the execution of operations on the contents of these variables that is not either impossible or program logical reasons make sense.
  • Type conversion (german type conversion), ie transformation or transport and degradation of types (german type promotion, demotion type ). For this purpose are the three ways cast up, cast down and cross cast available in many object-oriented languages ​​.

Examples

The following examples serve to illustrate the classification; the classification of languages ​​in these categories, however, is to some degree subjective ( C , for example, is more strongly typed than C, but weaker than OCaml ), and many type systems have, depending on the use of multiple aspects ( Polymorphic values ​​, type conversion, etc.).

Static, implicitly, strong: ocaml

Let MyAdd ab = (* We define a function that takes two unspecified values ​​*)     a . b;; (* . , The addition operator for floating point numbers *)   (* Val MyAdd: float - > float -> float = *)   (* Ocaml has recognized saying that this function takes two floating point numbers and returns a *)   MyAdd 1 2.5; ; (* Add up two numbers, but one of them is an integer *)   ( *'' 'Compiler Error: '' ' This expression Has type int but is here used with type float * )   MyAdd ( float_of_int 1 ) 2.5; ; (* Add up two numbers, integer to convert previously in floating point results in 3.5 * )       let x = object method a = 10 end;; (* Define an object with a method a, which returns the value 10 * )   let y = object method a = 10 b = 11 method end;; (* Define an object with two methods a and b, each returning different Ints. *)   let l = [x, ( y: > ) ]; ; (* Save x and y in a list. Y is in the type of x thereby converted *) Static, explicitly, weak: C

Int x; / / Assign x explicitly the type int ( integer) to.   printf (" % f", x); / / Return undefined x as a float (without conversion, result,                    / / Possibly program termination ) Dynamic, implicitly, weak: PHP

$ x = 1; / / Way to explicitly specify the value x 1.   $ x = 2 " 42"; / / Add a number and a string,                            / / Convert to the string to a number   / / $ X is now 44   $ x = 2 "I am not a number "; / / As above, invalid string is converted to 0   / / $ X is now 2 Dynamic, implicitly, strong: Python

X = 1 # way to explicitly specify the value x 1.   x = 2 " 42 " # Add up a number and a string, run-time error (" TypeError " )   x = 2 int ( " 42 ") # Add up a number and a number that is created from a string   # X is now 44   x = 2 int ("I am not a number ") # runtime error ( " ValueError " ) Static and dynamic, explicit, strong: Pike

Int x = 1; / / Define x as an int and as to explicitly specify the value 1.   x = 2 - " 42"; / / Subtract a string by a number, compile or runtime error ( "Bad argument 2 to ` -. " )   x = " 42"; / / Assign x the value " 42" to. Compile error ( "Bad type in assignment" )   x = 2 ( int) " 42"; / / Add a number and a number that is created from a string   / / X is now 44   x = 2 (int ) "I am not a number " / / Invalid character string is converted to 0   / / X is now 2 see also

  • Data type
  • Program Analysis
  • Type safety
717358
de