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

N

not_a_commie

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 TT> where 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?
 
B

Ben Voigt

not_a_commie said:
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<>".
3. I want to limit it to a generic with a subset of types: "where T :
List<new TT> where 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 said:
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 :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top