469,271 Members | 1,586 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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

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
1 2541

"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.

Similar topics

16 posts views Thread by middletree | last post: by
3 posts views Thread by A.V.C. | last post: by
29 posts views Thread by C A Upsdell | last post: by
5 posts views Thread by Mr. SweatyFinger | last post: by
2 posts views Thread by Jim.Mueksch | last post: by
5 posts views Thread by pwiegers | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.