Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Issue

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I was wondering if the following is possible using generics:

What i have now:




what I want:



So, the question pretty much is whether or not there is a way to tell the map that it's values are restricted to the type of the key parameter.

thanks
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I don't think so. I think you're pretty stuck there without writing your own Map-like structure. Generic restrictions are designed to apply to all objects in the collection, whereas you want to have the restriction depend on the position in the collection.

You could always write your own wrapper class. I'm not sure how complicated it would get, but it would start off something like this:
Whether that's worth the trouble, I'm unsure.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply ...

the structure is fully encapsulated so there is no real problem. i just figured it would be a nicer way to do it like that :-)
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic