Type system

This is an old revision of this page, as edited by TakuyaMurata (talk | contribs) at 02:29, 22 March 2003 (add some). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer science, datatypes (often simply called type) are a concept that clarifies contexts for numerical and other operations, limits destructive and uesless operations and provides semantic documentations to the programming languages.

Types are usually associated either with values in memory or with object (computer science)s such as variable (computer science)s. Because any value is simply a bunch of bits for computers, there is no distinction in hardware leven among memory address, instruction code, characters, integers and floating-point number. Numerical and string constants and expression in code can and often imply type in particular context. For example, an expression 3.14 implies its type is real while [1, 2, 3] implies type is a list of integer mostly as an array.

Type system allows operations can be done relying on contexts by type. For example, in an arithmetic expression, a + b, if a and b are typed as integer, an underlying operation can be integer addition. If the type is real, floating-point addition is probably done. In generics the type of values determines which code will be executed.

Besides, types make impossible to code some operations which cannot be valid in certain context. This mechanism effectively catches the majority of common mistakes made by programmers. For example, an expression "Hello, wikipedia" / 3 is invalid because a string literal cannot be divided by integer in usual sense.

Using types in languages also improves documentation of code. For example, the declaration of a variable with type comments code that variable is regared in context. how In fact, many languages provide the way that programmers define semantic type derived from builtin types.

While some languages use types in compile-type and don't have them in run-time, type information can be stored in memory for use in run-time. Many OOP languages keep certain information about type in run-time to make possible dynamic binding.

Some, particularly script languages and assembly languages provide no type. Object-oriented programming languages mostly support a type system by classes.

The process of verifying types is called type checking. If it occurs in compile-time, the whole type system is called statically typed. In turn, if it does in runtime, the type system called dynamically typed. C, Java and Pascal are statically typed while most of script languages include Perl, Ruby and Python. One of primary tasks of semantic analysis is type checking.

If type A is compatible with type B, A is a subtype of B while not always vice versa. See subtype for detail.

Types are generally divied into primitive types (also called built-in type or basic type) and composite types. Typical primitive types include integer, floating-point number (known as real). Sometimes string literal is included as well. Composite types, as the name implies, are types consisting of basic types. They are also called a record or structure type.