The Artima Developer Community
Sponsored Link

Artima Developer Spotlight Forum
Using Wildcards in Java Generics

23 replies on 2 pages. Most recent reply: Jul 8, 2008 8:37 AM by Carson Gross

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 23 replies on 2 pages [ « | 1 2 ]
James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Using Wildcards in Java Generics Posted: Jul 3, 2008 1:39 PM
Reply to this message Reply
Advertisement
> > basically using Java generics as if they worked like
> > arrays in Java. This is actually one way that generics
> > could have been implemented in Java and I've come to
> the
> > conclusion that that would have been the right choice.
> Except that arrays are run time type checked on
> assignment; an option not available with erasure. I would
> be rather uncomfortable with allowing unchecked stores
> into covariant types.

Well, erasure is the other mistake. I personally don't really want to get into whether erasure was necessary or not but you'll probably find people who do if you really want to.

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: Using Wildcards in Java Generics Posted: Jul 3, 2008 2:33 PM
Reply to this message Reply
Thinking about it more over a late lunch.

In general, I don't write into Collections passed in from the outside. I create my own. Call it "functional-like", or fear/confusion whether it is o.k. to write into their collection, something very few programmers document very well in my experience. When I do write into other Collections, it's a well defined concrete type like String, or a completely ill-defined type like Object. So I never/seldom need "super".

In general, our method signatures are Collection<FairlyAbstractType>, not Collection<SpecificType>. If everybody has been consistent with FairlyAbstractType and your code is well factored/designed (or you are just plain lucky!), we don't need extends. Probably should use it more, but don't. The knowledge that the collection is really of <SpecificType> is "in the comments" or "understood", and once in a while we do the unsafe casts. While James is right that in an ideal world it would be best to bring this knowledge to the compiler, I confess to James and agree with Alan that after a point it's not worth it.


p.s.

Re-reading this post, seems to me that perhaps "? extends" should have been "readable-as" and "? super" sould have been "writable-as". And, "readable-as" should have been the default cause that's what I use the most. :-)

Carson Gross

Posts: 153
Nickname: cgross
Registered: Oct, 2006

Re: Using Wildcards in Java Generics Posted: Jul 3, 2008 5:02 PM
Reply to this message Reply
> Except that arrays are run time type checked on
> assignment; an option not available with erasure. I would
> be rather uncomfortable with allowing unchecked stores
> into covariant types.

Yeah, it's an uncomfortable choice, but I think it is the pragmatic one. You sacrifice some type safety for a lot less syntactic and conceptual burden. It's a tough trade-off, but I'm pretty convinced it's the right one now.

I think Morgan touched on an important point: you usually aren't writing into Collections you are on the "receiving" end of, which is a good example of the dictum that reads tend to outweigh writes in a given program. Given that, covariance is usually the right thing.

Also, as Fred Garvin points out, keep in mind that even the wildcard approach leaves some type-safety on the floor: methods like Map.get() take an Object rather than a K because of it. In a covariant-generics regime, you can fix this.

Cheers,
Carson

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Using Wildcards in Java Generics Posted: Jul 3, 2008 7:14 PM
Reply to this message Reply
> While James is
> right that in an ideal world it would be best to bring
> this knowledge to the compiler, I confess to James and
> agree with Alan that after a point it's not worth it.

I'm actually not saying that it's a good idea to make it type safe. The more I think about your approach, the more I like it. It's ugly but does resolve a number of issues with generics.

My point is that you are basically demonstrating complete disregard (disgust?) for the over-complicated Java generics implementation. I really wonder how many other shops are doing this.

I just think Java generics would be better if it supported your approach properly.

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: Using Wildcards in Java Generics Posted: Jul 4, 2008 10:58 AM
Reply to this message Reply
> I'm actually not saying that it's a good idea to make it
> type safe. The more I think about your approach, the more
> I like it. It's ugly but does resolve a number of issues
> with generics.

I prefer "Pragmatic". :-)

Maybe time to write one of those "Pragmatic XXX" books...

Gregor Zeitlinger

Posts: 108
Nickname: gregor
Registered: Aug, 2005

Re: Using Wildcards in Java Generics Posted: Jul 4, 2008 12:44 PM
Reply to this message Reply
> I wonder how prevalent this is because it's
> > basically a pretty damning critique of Java generics.
>
> I think you get more than just "no casts"
I think the main benefit that it makes signatures easier to read and hence development faster.

I doesn't ussually avoid errors, though.
I've auto-generified 20,000 classes and only found a few class cast exceptions in code that was not used.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Using Wildcards in Java Generics Posted: Jul 4, 2008 1:01 PM
Reply to this message Reply
> > I'm actually not saying that it's a good idea to make
> it
> > type safe. The more I think about your approach, the
> more
> > I like it. It's ugly but does resolve a number of
> issues
> > with generics.
>
> I prefer "Pragmatic". :-)
>
> Maybe time to write one of those "Pragmatic XXX" books...

Pragmatic triple-X? There might be some real money in that.

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

So, how do others handle them? Posted: Jul 7, 2008 1:37 PM
Reply to this message Reply
Any secrets or advice to make using wildcards more tractable?

Carson Gross

Posts: 153
Nickname: cgross
Registered: Oct, 2006

Re: So, how do others handle them? Posted: Jul 8, 2008 8:37 AM
Reply to this message Reply
> Any secrets or advice to make using wildcards more
> tractable?

We wrote our own JVM language and then adopted covariance of parameterized types in it to eliminate wildcards. That might be a bit extreme, though. <smile/>

I don't see much of a way out other than to revert to generic lists.

Cheers,
Carson

Flat View: This topic has 23 replies on 2 pages [ « | 1  2 ]
Topic: Using Wildcards in Java Generics Previous Topic   Next Topic Topic: Search Engines to Make Flex Content Indexable

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use