Nominative type system
From Wikipedia, the free encyclopedia
In computer science nominative type system is a major class of type system, in which type compatibility and equivalence is determined by explicit declarations and/or the name of the types. Nominative systems are used to determine if types are equivalent, as well as if a type is a subtype of another. It contrasts with structural systems, where comparisons are based on the structure of the types in question and do not require explicit declarations.
Nominal typing means that two variables have compatible types only if they appear in either the same declaration or in declarations that use same type name. Note that many languages provide a way of type aliasing; or declaring multiple names of the same type. The C/C++ typedef feature is an example of this capability. C# and Java are all examples of languages which use name type compatibility.
In a similar fashion, nominal subtyping means that one type is a subtype of another if and only if it is explicitly declared to be so in its definition. Nominally-typed languages typically enforce the requirement that declared subtypes be structurally compatible (though Eiffel allows non-compatible subtypes to be declared); however subtypes which are structurally compatible "by accident" but not declared to be subtypes, are not considered to be subtypes.
C, C++, C# and Java all primarily use both nominal typing and nominal subtyping. C and C++ permit limited structural subtyping for anonymous types (two anonymous types are considered equivalent if they have the same structure, even if the types are "described" in different parts of the code).
Some nominally-subtyped languages, such as Java and C#, allow classes to be declared final (or sealed in Microsoft's terminology), indicating that no further subtyping is permitted.
Nominal typing is useful at preventing accidental type equivalence, and is considered to have better type-safety than structural typing. The cost is a reduced flexibility, as, for example, nominal typing does not allow new super-types to be created without modification of the existing subtypes.
[edit] See also
[edit] External links
- Types and Programming Languages by Benjamin Pierce (MIT Press 2002) [1]
- c2.com: Nominative and structural typing