• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Purpose of Wild Card in Reference

 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja, that's a better example. You call some method that returns a generic type with a specific parameter. You can still work around that using a base type but with generics the types might not be related. You must then use two different references or the wild card. The wild card is better in that case. I'm in agreement on that.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rick,
You can delete one of your own posts by clicking on the "edit" icon (the one with the pencil and paper) and clicking on the checkbox (which is labelled "Delete Post? (check box, if yes)") that appears on the side of the textarea (where you would normally make your edits). Then click on the "Edit Post" button, and voila! It's gone.
[ August 15, 2005: Message edited by: Marilyn de Queiroz ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreement!

Thanks for starting the discussion, by the way, I learned a lot on the way (even if it was a little bit painful from time to time)!
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I also increased my generics knowledge substantially just poking around for usage patterns.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you recall any facility for marking types covariant, contravariant or bivariant? There are none, that is to say you cannot stipulate that a given parameter is write-only, read-only or read-write.

Covariant doesn't mean read-only or write-only, although it implies that in the context of simple references (which is the only context in which I've used the terms read/write-only). A type parameter in a type constructor is covariant if subtypes of the constructed type must have subtypes of the type parameter as parameters.

In the following declaration the type parameter of Foo is marked as being covariant:

Foo<? extends Bar> localVariable;

All that covariant means in this context is that I can assign Foo<Baz> to localVariable iff Baz is a subtype of Bar. So yes, the type of localVariable here has been specficially annotated to allow covariant subtypes.

I don't think we're actually in disagreement over anything mechanical, but I think we've run into linguistic problems. When you say wildcard, I believe you're thinking of the wildcard syntax and where it appears and the existential restrictions it applies. When I say wildcard I think of the entire wildcard feature, including subtyping rules and non-wildcard types.

In practice I believe you can almost always avoid converting an invariant type to a wildcarded type in a local variable if you don't want wildcards in local variables. I just think that avoiding wildcards in local variables is an strange stylistic goal, particularly considering the possible negative effects when it comes time to modify your code to make it more general. Since I really have nothing more to say on the topic than that I'll leave off here.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was borrowing this context in this article.

http://www.onjava.com/pub/a/onjava/2003/09/24/readable_java.html?page=last

* A covariant, or read-only list of Numbers as List<+Number>.
* A contravariant, or write-only list of Numbers as List<-Number>.
* A bivariant, read-write list of Numbers as List<*>.
* An invariant list of Numbers as List<=Number>.


When I think covariant I think overloading on return type as can be done with inheritance in Java 5.0.
[ August 15, 2005: Message edited by: Rick O'Shay ]
 
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic