Datatype
Editorial note: programming language also has discussion of type system.
In computer science, a datatype (often simply called type) is a statically assigned constraint on a programming language phrase that denotes the kinds of values it may take on and gives it certain semantic meaning for the purposes of preventing errors, building abstractions, documenting the program, and gaining some measure of runtime safety and efficiency. A type system provides a method for reasoning about program behavior based on type rules, which specify the ways in which typed program phrases can legally interact. The study of type systems is known as type theory. Programming languages which provide type systems are known as typed languages. Although the majority of programming languages are typed, some, known as untyped langugages, do not provide types.
| Table of contents |
|
2 Compile-time and run-time 3 Categories of types 4 Compatibility, equivalence and substitutability 5 Type checking 6 See also |
The basic idea of typing is to give mere bits semantic meaning. Types are usually associated either with values in memory or with objectss such as variabless. Because any value is simply a set of bits for computers, there is no distinction in hardware even among memory addresses, instruction code, characters, integers and floating-point numbers. Numerical and string constants and expressions in code can and often do imply type in a particular context. For example, an expression
In some languages, such as C and Java, some types are associated with the particular implementation. For example, in Java, type
The type system allows operations to be done relying on contexts by type. For example, in an arithmetic expression,
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
Using types in languages also improves documentation of code. For example, the declaration of a variable as being of a specific type documents how the variable is used. In fact, many languages allow programmers to define semantic types derived from builtin types; either composed of elements of one or more builtin types, or simply as aliases for names of builtin types.
Datatypes may be of first-class, second-class or third-class value.
While some languages use types during compile-time and do not have them during run-time, type information can be stored in memory for use during run-time. Many OOP languages keep certain information about type at run-time to make possible dynamic binding. In C++, such information is called RTTI.
Types can be classified with following categories:
The question of compatibility and equivalence is a complicated and controversial topic and it is related to the problem of substitutionality: that is, given type A and type B, are they equal types? compatible? can the value with type B be used in the place where the value of A?
If type A is compatible with type B, A is a subtype of B while not always vice versa. The definition is known as Liskov substitution principle.
The process of verifying types is called type checking. If it occurs at compile-time, the whole type system is called statically typed. If it occurs at run-time, the type system is called dynamically typed. C, Java and Pascal are statically typed while most script languages, including Perl, Ruby and Python, are dynamically typed. One of the primary tasks of semantic analysis is type checking. In dynamic scope, type checking must be done at run-time because variables can be differently typed according to execution path.
Static type checking system usually assign a single type to each syntactic program entity (e.g., each bound variable name or expression). This is in contrast to dynamically typed systems, which do not require that syntactic entities be consistently typed.
Consider the following pseudocode example:
Some statically typed languages, notably C and its derivatives, have a "back door" in the language that enables programmers to write type-incorrect code by deliberately circumventing the static type system. Languages with back doors are called weakly typed; languages without back doors are called strongly typed.
The presence of static typing in a programming language does not necessarily imply the absence of dynamic typing mechanisms. For example, Java is statically typed, but certain operations require the support of runtime type tests, which are a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.
Widely known programming languages with static typing include the following: ML, C (a procedural programming language), Java.
The implementation of a dynamically typed language will catch errors related to the misuse of values---"type errors"---at the time the erroneous statement or expression is computed. In other words, dynamic typing catches errors during program execution. A typical implementation of dynamic typing will keep all program values "tagged" with a type, and checking the type tag before any value is used in an operation.
For example, consider the following pseudocode:
Well-known dynamically typed languages, in each of the major language paradigms, include the following: Lisp and its dialects, Perl, Smalltalk, Ruby, Python, Visual Basic
The choice between static and dynamic typing requires some trade-offs. Static type disciplines operate on program source code rather than on the program execution. Therefore, they are able to detect certain kinds of errors without executing the program This "early detection" of errors is one of the key software engineering benefits of statically typed systems.
Dynamic typing sometimes simplifies the task of writing code, because it allows the programmer to write code that would be illegal in some static type systems. Also, certain language constructs (for example, an eval function that can execute arbitrary data as code) are difficult to provide in a purely statically typed language.
However, purely dynamically typed languages provide only "late detection" of errors---errors may not be detected until the program is actually run. This complicates both the task of verifying that code is correct a priori, and the task of debugging code a posteriori when errors do arise. Dynamic typing advocates claim that the benefits of flexibility outweigh these disadvantages, and seek to detect the sort of errors that would be detected by strong typing by using extensive unit testing.Basis
3.14 implies its type is floating-point while [1, 2, 3] implies type is a list of integers; typically an array.int is defined as a 4-byte signed integer. On the other hands, some languages only define the semantic behavior of all types.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."Hello, Wikipedia" / 3 is invalid because a string literal cannot be divided by an integer in the usual sense.Compile-time and run-time
Categories of types
Compatibility, equivalence and substitutability
Type checking
Static type checking
var x; // (1)
x = 5; // (2)
x = "hi"; // (3)
In this example, (1) declares the name x; (2) binds the integer value 5 to the name x; and (3) binds the string value "hi" to the name x. A typical static type discipline would require that the name x be assigned a single type, and hence that all values bound to x be of the same type. In such a system, the above code fragment would be illegal, because (2) and (3) bind x to values of inconsistent type (in most type systems, no value can be both an integer and a string). By contrast, a purely dynamically typed system would permit the above program to execute, because the name x would not be required to have a consistent type.Dynamic typing
var x = 5; // (1)
var y = "hi"; // (2)
x + y; // (3)
In this code fragment, (1) binds the value 5 to x; (2) binds the value "hi" to y; and (3) attempts to add x to y. In a dynamically typed language implementation, the value bound to x might be a pair (integer, 5), and the value bound to y might be a pair (string, "hi"). When the program attempts to execute line (3), the language implementation would check the type tags integer and string, discover that the operation + (addition) is not defined over these two types, and signal an error. However, if this is a weakly-typed language, such as Visual Basic, the code would run properly, yielding the result "5hi". There are problems to weakly typed languages, though. For example, would the result of the following code be 9 or "54"?var x = 5;
var y = "4";
x + y
Many say that weak typing gets programmers into bad habits because it doesn't teach them to use explicit type conversion.The Controversy between static and dynamic typing






