For faster navigation, this Iframe is preloading the Wikiwand page for 名义型别系统.

名义型别系统

此条目需要补充更多来源。 (2024年3月24日)请协助补充多方面可靠来源改善这篇条目无法查证的内容可能会因为异议提出而被移除。致使用者:请搜索一下条目的标题(来源搜索:"名义型别系统"网页新闻书籍学术图像),以检查网络上是否存在该主题的更多可靠来源(判定指引)。

计算机科学中,如果一个类型系统名义的nominal)(也称为名称基础的name-based)),则数据类型的兼容性和等价性是通过明确地宣告和/或类型的名称来决定的,称之为名义型别系统(英语:nominal type system)。名义系统用于决定类型是否相等,以及一个类型是否为另一个类型的子类型。名义类型系统与结构系统英语structural systems相对,后者基于类型的结构进行比较,不需要明确宣告。

名义型别

名义型别代表两个变数仅在它们的宣告命名相同类型时才具有型别相容性。例如在C语言中,同个翻译单元中具有不同名称的两个 struct 型别永不被视为相容,即使它们具有相同的字段声明。 然而,C也允许 typedef 宣告,引入了现有类型的别名。这些仅是语法上的,对于型别检查不会区分类型和其别名。这个特性在许多语言中存在,当(例如)同一个原始整数型别在两个语义上不同的情况下使用时,可能会导致型别安全性的损失。Haskell 提供了 C 风格的语法别名,形式上是 type 宣告,以及newtype 宣告,确实引入了一个新的、不同的类型,与现有类型同构[1]

名义子类型别

同样地,名义子类型表示一个类型是另一个类型的子类型,仅当在其定义中明确宣告为如此时才成立。名义型语言(nominally-typed languages)通常会强制要求宣告的子类型在结构上相容(尽管 Eiffel 允许声明非相容的子类型)。然而,那些在结构上相容但未宣告为子类型的子类型则不被认为是子类型。

C++C#JavaObjective-CDelphiSwiftJuliaRust 等语言主要都使用名义型别和名义子类型别。

一些具有名义子类型的语言,像是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}}
名义型别系统
Listen to this article

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:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

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}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?