Nominal type system
In computer science, a type system is a nominal or nominative type system (or name-based type system) if compatibility and equivalence of data types is determined by explicit declarations and/or the name of the types. Nominal systems are used to determine if types are equivalent, as well as if a type is a subtype of another. Nominal type systems contrast 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 are type-compatible if and only if their declarations name the same type. For example, in C, two
struct types with different names in the same translation unit are never considered compatible, even if they have identical field declarations.
However, C also allows a
typedef declaration, which introduces an alias for an existing type. These are merely syntactical and do not differentiate the type from its alias for the purpose of type checking. This feature, present in many languages, can result in a loss of type safety when (for example) the same primitive integer type is used in two semantically distinct ways. Haskell provides the C-style syntactic alias in the form of the
type declaration, as well as the
newtype declaration that does introduce a new, distinct type, isomorphic to an existing type.
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 as subtypes, are not considered to be subtypes.
Some nominally-subtyped languages, such as Java and C#, allow classes to be declared final (or sealed in C# terminology), indicating that no further subtyping is permitted.
Nominal typing is useful at preventing accidental type equivalence, which allows 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.
- "Haskell 2010 Report: Declarations and Bindings: User-Defined Datatypes". Archived from the original on 2017-01-04. Retrieved 2015-06-02.
- Pierce, Benjamin C. (2002). "§19.3 Nominal and Structural Type Systems". Types and Programming Languages. MIT Press. pp. 253–. ISBN 0-262-16209-1.