Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Nominal type system

From Wikipedia, the free encyclopedia
Major class of type system in computer science
Type systems
General concepts
Major categories
Minor categories

Incomputer science, atype system isnominal (also callednominative orname-based) if compatibility and equivalence ofdata types is determined by explicit declarations and/or the name of the types. Nominal systems are used to determine whether types are equivalent, as well as whether a type is a subtype of another. Nominal type systems contrast withstructural systems, where comparisons are based on the structure of the types in question and do not require explicit declarations.

Nominal typing

[edit]

Nominal typing means that two variables are type-compatible if and only if their declarations name the same type. For example, inC, twostruct types with different names in the same translation unit are never considered compatible, even if they have identical field declarations.

However, C also allows atypedef declaration, which introduces an alias for an existing type. These are merelysyntactical 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 thetype declaration, as well as thenewtype declaration that does introduce a new, distinct type,isomorphic to an existing type.[1]

Nominal subtyping

[edit]

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 (thoughEiffel 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.

C++,C#,Java,Kotlin,Objective-C,Delphi,Swift,Julia andRust all primarily use both nominal typing and nominal subtyping.

Some nominally-subtyped languages, such as Java and C#, allow classes to be declaredfinal (orsealed in C# terminology), indicating that no further subtyping is permitted.

Comparison

[edit]

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.

See also

[edit]

References

[edit]
  1. ^"Haskell 2010 Report: Declarations and Bindings: User-Defined Datatypes". Archived fromthe original on 2017-01-04. Retrieved2015-06-02.

Sources

[edit]

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Nominal_type_system&oldid=1281351682"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp