Jump to content

Talk:Intersection type

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Java really supporting intersection types?

[edit]
Language Actively developed Paradigm(s) Status Features
Java Yes[1] Supported[2]
  • Type refinement
  • Interface composition
  • Subtyping in width

Well, I think Java only supports intersection types to some extent. A language implementing true intersection types enables to insert intersection expressions into type positions. This isn't the case for Java, see this example (JSHELL JDK 15):

jshell> interface i1{}
|  created interface i1

jshell> interface i2{}
|  created interface i2

jshell> i1 & i2 = null
|  Error:
|  cannot find symbol
|    symbol:   variable i1
|  i1 & i2 = null
|  Error:
|  cannot find symbol
|    symbol:   variable i1
|  i1 & i2 = null
|  ^^
|  Error:
|  cannot find symbol
|    symbol:   variable i2
|  i1 & i2 = null
|       ^^

Iconsize (talk) 12:53, 19 March 2021 (UTC)[reply]

I don't think the specific syntax a language uses matters to the concepts it supports. In this case you simply didn't write correct Java.
jshell> interface i1 {}
|  created interface i1

jshell> interface i2 {}
|  created interface i2

jshell> interface i3 extends i1, i2 {}
|  created interface i3

jshell> i3 x = null
x ==> null

jshell> i1 a = x
a ==> null

jshell> i2 b = x
b ==> null

jshell> a = b
|  Error:
|  incompatible types: i2 cannot be converted to i1
|  a = b
|      ^
This doesn't mean that Java allows you to do everything intersection types allow you to do, but it does – as you said – allow a restricted form of it.
Kavuldra (talk) 14:07, 22 January 2022 (UTC)[reply]

References

  1. ^ "Java Software". Retrieved 2019-08-08.
  2. ^ "IntersectionType (Java SE 12 & JDK 12)". Retrieved 2019-08-01.