• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't understand an erasure feature from generics  RSS feed

 
Arthur Vinicius Rebelo
Ranch Hand
Posts: 30
1
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, reading the JLS 7 I found myself trapped in this line right here :
The erasures of all constituent types of a bound must be pairwise different, or a compile-time error occurs.

Well I know what type erasure is, and I think I kind got what this statement means. My understanding from it is that if your type parameter has more than one bound and those bounds occurs to be the same type after erasure that is a compile-time error. Is that it?

The only thing I could found related is something like this:

Which as you might know gives the Duplicated bound error.

Thank you in advance.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct.

Where exactly in the specification did you find this sentence? (Include a link to the paragraph or chapter if possible). With some more context it would be a lot easier to help you understand this.
 
Arthur Vinicius Rebelo
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from chapter 4, more specifically in this paragraph. The context is pretty much generics in Java and the rules for multiple bounds in Type Parameter
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are correct. But there's more: generic types are erased by the compiler. That means something like this will also produce a compile error:

Because when the generic types are erased, both List<Integer> and List<String> become List - and according to the rule you quoted, that is not allowed.
 
Arthur Vinicius Rebelo
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks. I'm more confident about it now but your example I think goes under another rule from type variable, take a look at the next rule right below.
A type variable must not at the same time be a subtype of two interface types which are different parameterizations of the same generic interface, or a compile-time error occurs.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note, I never understood the need for the phrase "pairwise different". Isn't the phrase "different" good enough? ... meaning if you have a set of numbers, and I say that they are different, don't most people think that there are no equal numbers in the set? Or is the common understanding that all the numbers in the set are not the same value, but a few of them may be the same?

It just seems to be an unnecessary clarification. What does everyone else think?

Henry
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have just said "all different" if I felt that clarification of "different" was necessary (which I'm not sure if I feel that way).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!