• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Generic declaration question

 
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I'm wondering if there is any difference between these two method declarations, given that <T> is defined on the class, and this is the only overt use of X:





I can see that if there were some other use of X, then ? might meaningfully be a subtype of X, while X is "pinned" somewhere between Throwable and the value inferred for ?, so in this:



one might have two different suppliers, requiring the ? extends x format. But in the first two examples, it seems like an arbitrary choice. Am I missing something?

TIA
Toby
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you are talking about the implementation in the Java8 Optional, you missed a part of the method signature:



As I read it, this signature says that the method returns a T, or else throws an X (it declares that it will throw an exception of type X). The provided Supplier, however, can return any subclass of X. For example, if we fill in some of those parameters, this would be valid:



whereas with the other approach (using Supplier<X> instead), the Supplier's type and the type of exception declared as thrown much match up.

Unfortunately, that's about as much as I can puzzle out. I can't think of any scenario where you could get the compiler to infer something like the above, or where it would be meaningful. You can throw and catch super classes of an exception just fine already... Hmmm...
 
Toby Eggitt
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, right! Of course. I had missed that, so my own explanation kinda hit the point (that there might be some other X while ? represented a subclass). Phew, sanity restored, I believe you hit the nail on the head

Many thanks!
Toby
 
What's that smell? Hey, sniff this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic