How is Java "almost entirely nominally typed"?

I read from this article that "Java is almost entirely nominally typed." So there are structurally typed parts in Java.

What are the nominal and structural parts of a Java type system?

+3


source to share


2 answers


As per an earlier post by the same author - removing function types :

  • There are two main approaches to recruitment: nominal and structural. The identity of the denomination is based on its name; the identity of a structured type is based on what it is made of (for example, "tuple int, int" or "function from int to float".)

Most languages ​​choose mostly nominal or mostly structural; There are not many languages ​​that successfully combine nominal and structural typing other than "at the edges". Java is almost entirely nominated (with a few exceptions: arrays are a structural type , but at the bottom there is always a nominal element type; generics are nominal and structural too , and this is actually part of the source many people complain about generics.)



So arrays and generics parts are structured types.

I think structured types could be something like <T extends A & B>

being a supertype <T extends A>

, or Object[]

being a supertype String[][]

. These type compatibility is not based solely on their name.

+2


source


As far as I can tell, Java is completely nominally typed. Two objects are type compatible if they have the same named type. In Java, it declares classes:

class A {
    public int value;
}

class B {
    public int value;
}

      



doesn't give you anything that would cause language constructs to use equal members declared in the same order.

Whereas in C you can use (under certain circumstances) declarations A

and B

how struct

, and having the same binary layout means you can copy them over each other and shape union

and use that overlap.

+1


source







All Articles