For faster navigation, this Iframe is preloading the Wikiwand page for
名义型别系统.
在计算机科学中,如果一个类型系统是名义的(nominal)(也称为名称基础的(name-based)),则数据类型的兼容性和等价性是通过明确地宣告和/或类型的名称来决定的,称之为名义型别系统(英语:nominal type system)。名义系统用于决定类型是否相等,以及一个类型是否为另一个类型的子类型。名义类型系统与结构系统相对,后者基于类型的结构进行比较,不需要明确宣告。
名义型别
名义型别代表两个变数仅在它们的宣告命名相同类型时才具有型别相容性。例如在C语言中,同个翻译单元中具有不同名称的两个 struct
型别永不被视为相容,即使它们具有相同的字段声明。
然而,C也允许 typedef
宣告,引入了现有类型的别名。这些仅是语法上的,对于型别检查不会区分类型和其别名。这个特性在许多语言中存在,当(例如)同一个原始整数型别在两个语义上不同的情况下使用时,可能会导致型别安全性的损失。Haskell 提供了 C 风格的语法别名,形式上是 type
宣告,以及newtype
宣告,确实引入了一个新的、不同的类型,与现有类型同构。[1]
名义子类型别
同样地,名义子类型表示一个类型是另一个类型的子类型,仅当在其定义中明确宣告为如此时才成立。名义型语言(nominally-typed languages)通常会强制要求宣告的子类型在结构上相容(尽管 Eiffel 允许声明非相容的子类型)。然而,那些在结构上相容但未宣告为子类型的子类型则不被认为是子类型。
C++、C#、Java、Objective-C、Delphi、Swift、Julia 和 Rust 等语言主要都使用名义型别和名义子类型别。
一些具有名义子类型的语言,像是Java和C#,允许类别被声明为 final(或在C#中称为 sealed),表示不允许进一步的创建子类型。
比较
名义型别在防止意外之类型等价方面很有用,这比结构型别提供了更佳的型别安全性。但其代价是减少了灵活性,例如名义型别不允许在不修改现有子类型的情况下创建新的父类型(super-types)。
资源
- Pierce, Benjamin C. §19.3 Nominal and Structural Type Systems. Types and Programming Languages. MIT Press. 2002: 253–. ISBN 0-262-16209-1.
{{bottomLinkPreText}}
{{bottomLinkText}}
This page is based on a Wikipedia article written by
contributors (read/edit).
Text is available under the
CC BY-SA 4.0 license; additional terms may apply.
Images, videos and audio are available under their respective licenses.
{{current.index+1}} of {{items.length}}
Thanks for reporting this video!
This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:
An extension you use may be preventing Wikiwand articles from loading properly.
If you're using HTTPS Everywhere or you're unable to access any article on Wikiwand, please consider switching to HTTPS (https://www.wikiwand.com).
An extension you use may be preventing Wikiwand articles from loading properly.
If you are using an Ad-Blocker, it might have mistakenly blocked our content.
You will need to temporarily disable your Ad-blocker to view this page.
✕
This article was just edited, click to reload
Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}
Follow Us
Don't forget to rate us
Oh no, there's been an error
Please help us solve this error by emailing us at
support@wikiwand.com
Let us know what you've done that caused this error, what browser you're using, and whether you have any special extensions/add-ons installed.
Thank you!