Jump to content
  • Advertisement
Sign in to follow this  
jfclavette

[.net] A few generics questions.

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two unanswered questions about generics. I'm led to believe that the features are not implemented in any way, but here goes. 1. Is there a way to do something that would achieve similar functionnality to this (made up syntax):
  class Foo<T1, T2, ..., Tn, W<T2>> {} 

  ...
  
  //later
  Foo<R1, R2, ..., Rn, W> bar;
and have it that W must be a Generic type which can take R2 as a type parameter ? Specifically, say I want to pass a key type, a value type and an arbitrary collection implementing IDictionnary. It would look something like:
  class Foo<KeyType, ValueType, AssociativeCollection<KeyType, ValueType> 
    where AssociativeCollection: IDictionnary<KeyType, ValueType>
 {} 

  ...
  
  //later
  Foo<int, float, MyDictionnaryImplementation> bar;
and avoid crazy long lines with redundant type parameters ? 2. Is there a way to specify a default type argument so that it can be ommited ? [Edited by - jfclavette on July 28, 2006 10:34:25 PM]

Share this post


Link to post
Share on other sites
Advertisement
I think:

1. Eh, not really. You could probably use a generic method to fill in the types and return the full item, but it's probably simpiler just to use the constraint you already posted and double enter the types.

2. Eh, not really. You could do:

public class Foo<K,V,D> where D:IDictionary<K,V>{...}
public class Foo:Foo<int,int,Dictionary<int,int>>{...}


but that just creates two different class types, and would require special care in constructing the generic foo so that the default foo exposes everything similarly.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!