Java"almost entirely nominally typed"是什么方式?
In what way is Java "almost entirely nominally typed"?
我从 this article 读到 "Java is almost entirely nominally typed"。所以Java.
中有结构类型的部分
Java 类型系统的标称部分和结构部分是什么?
据我所知,Java 完全是名义上的类型。
如果两个对象具有相同的命名类型,则它们是类型兼容的。在 Java 中声明 类:
class A {
public int value;
}
class B {
public int value;
}
不会为您提供任何语言构造方式来利用以相同顺序声明的相等成员。
而在 C 中,您可以利用(在某些情况下)将 A
和 B
声明为 struct
s 并且它们具有相同的二进制布局,这意味着您可以将它们复制过来彼此形成 union
并利用重叠。
根据同一作者之前的 post - removal of function types:
- There are two basic approaches to typing: nominal and structural. The identity of a nominal is based on its name; the identity of a
structural type is based on what it is composed of (such as "tuple of
int, int" or "function from int to float".)
Most languages pick mostly nominal or mostly structural; there are not
a lot of languages that successfully mix nominal and structural
typing except "around the edges." Java is almost entirely nominal
(with a few exceptions: arrays are a structural type, but at the
bottom there is always a nominal element type; generics have a mix of
nominal and structural too, and this is in fact part of the source of
many of people's complaints about generics.)
所以,数组和部分泛型是结构类型。
我认为结构类型可能类似于 <T extends A & B>
是 <T extends A>
的超类型,或者 Object[]
是 String[][]
的超类型。这些类型兼容性不仅仅基于它们的名称。
我从 this article 读到 "Java is almost entirely nominally typed"。所以Java.
中有结构类型的部分Java 类型系统的标称部分和结构部分是什么?
据我所知,Java 完全是名义上的类型。 如果两个对象具有相同的命名类型,则它们是类型兼容的。在 Java 中声明 类:
class A {
public int value;
}
class B {
public int value;
}
不会为您提供任何语言构造方式来利用以相同顺序声明的相等成员。
而在 C 中,您可以利用(在某些情况下)将 A
和 B
声明为 struct
s 并且它们具有相同的二进制布局,这意味着您可以将它们复制过来彼此形成 union
并利用重叠。
根据同一作者之前的 post - removal of function types:
- There are two basic approaches to typing: nominal and structural. The identity of a nominal is based on its name; the identity of a structural type is based on what it is composed of (such as "tuple of int, int" or "function from int to float".)
Most languages pick mostly nominal or mostly structural; there are not a lot of languages that successfully mix nominal and structural typing except "around the edges." Java is almost entirely nominal (with a few exceptions: arrays are a structural type, but at the bottom there is always a nominal element type; generics have a mix of nominal and structural too, and this is in fact part of the source of many of people's complaints about generics.)
所以,数组和部分泛型是结构类型。
我认为结构类型可能类似于 <T extends A & B>
是 <T extends A>
的超类型,或者 Object[]
是 String[][]
的超类型。这些类型兼容性不仅仅基于它们的名称。