- Structural type system
A structural type system is a major class of
type system , in which type compatibility and equivalence are determined by the type's structure, and not through explicit declarations. Structural systems are used to determine if types are equivalent, as well as if a type is a subtype of another. It contrasts with nominative systems, where comparisons are based on explicit declarations or the names of the types, andduck typing , in which only the part of the structure accessed at runtime is checked for compatibility..In structural typing, two objects or terms are considered to have compatible types if the types have identical "structure". Depending on the semantics of the language, this generally means that for each feature within a type, there must be a corresponding and identical feature in the other type. Some languages may differ on the details (such as whether the "features" must match in name).
Haskell, ML, [http://ssdl-wiki.cs.technion.ac.il/wiki/index.php/Whiteoak Whiteoak] and Objective Caml are examples of structurally-typed languages. [http://haxe.org HaXe] uses structural typing, although classes are not structurally subtyped.
In languages which support subtype polymorphism, a similar dichotomy can be formed based on how the subtype relationship is defined. One type is a subtype of another if and only if it contains all the "features" of the base type (or subtypes thereof); the subtype may contain additional features (such as members not present in the base type, or stronger invariants).
Structural subtyping is arguably more flexible than nominative subtyping, as it permits the creation of "ad hoc" types and interfaces; in particular, it permits creation of a type which is a supertype of an existing type T, without modifying the definition of T. However this may not be desirable where the programmer wishes to create closed abstractions.
A pitfall of structural typing versus nominative typing is that two separately defined types intended for different purposes, each consisting of a pair of numbers, could be considered the same type by the type system, simply because they happen to have identical structure. One way this can be avoided is by creating one
algebraic data type for one use of the pair and anotheralgebraic data type for the other use.External links
* [http://www.cis.upenn.edu/~bcpierce/tapl/main.html "Types and Programming Languages" by Benjamin Pierce (MIT Press 2002)]
* [http://www.c2.com/cgi/wiki?NominativeAndStructuralTyping c2.com: Nominative and structural typing]
Wikimedia Foundation. 2010.