By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,649 Members | 2,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,649 IT Pros & Developers. It's quick & easy.

C# 3.0: no additions to where clause? dang.

P: n/a
I was hoping for increased functionality with the where clause in C#
3.0. Using the new keyword 'var' would really allow us to take nice
advantage of these. Specifically:

1. I want to limit it to primitives: "where T : System.Type.Primitive"
or something like that.
2. I want to limit it to an unspecified generic: "where T : List<>".
3. I want to limit it to a generic with a subset of types: "where T :
List<new TTwhere TT : class" or something similar.
4. I want to limit to a class with a certain property or method: "where
T : interface MyFunc(int), interface operator+=(int), interface get
MyProperty(int)" or something similar.
5. I want a default type: "where T default(MyType): IMyType" or
something similar so that I don't have to specify the type in all
situations. If it leads to ambiguous functions, throw an error.
6. Specify a namespace: "where T in System : class" or something like
that.
7. I want to AND/OR the conditions for the where clause together.

Thoughts? Comments? Complaints?

Jan 11 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"not_a_commie" <no********@gmail.comwrote in message
news:11*********************@i56g2000hsf.googlegro ups.com...
>I was hoping for increased functionality with the where clause in C#
3.0. Using the new keyword 'var' would really allow us to take nice
advantage of these. Specifically:

1. I want to limit it to primitives: "where T : System.Type.Primitive"
or something like that.
"where T : primitive" would be very nice, I agree
2. I want to limit it to an unspecified generic: "where T : List<>".
Just make S the generic parameter, you can use List<Sthroughout your
generic class
3. I want to limit it to a generic with a subset of types: "where T :
List<new TTwhere TT : class" or something similar.
Same as #2
4. I want to limit to a class with a certain property or method: "where
T : interface MyFunc(int), interface operator+=(int), interface get
MyProperty(int)" or something similar.
Since generics are shared between all reference types, this would only be
possible for type parameters restricted to value types. In that case it
would be extremely useful and perfect for generic arithmetic.

Would have to be syntax like:
where T : value having operator+=(T,T=>T)
5. I want a default type: "where T default(MyType): IMyType" or
something similar so that I don't have to specify the type in all
situations. If it leads to ambiguous functions, throw an error.
C# in general doesn't have default arguments but uses overloaded functions
to get the same user experience.

Generics can be overloaded based on the number of type arguments. So you
get what you're looking for with
public class Hashmap<T: Hashmap<string, T>
6. Specify a namespace: "where T in System : class" or something like
that.
7. I want to AND/OR the conditions for the where clause together.
The constraints are always and'ed, because if the compiler generates calls
in MSIL, the methods have to be available.

What would be useful would be constraint overloading:

class StableSortedList<T{ /* use a stable sort */ }
class SortedList<T{ /* use the fastest sort */ }
class StableSortedList<T: SortedList<T>
where T : ITotalOrdering, T : IUnique { /* if elements can never compare
equal, then needn't use the expensive stable sort algorithm */ }

The problem I see with that is that while the compiler could pick that
easily, it would tend not to propagate through other generics :(
>
Thoughts? Comments? Complaints?

Jan 11 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.