PC Review


Reply
Thread Tools Rate Thread

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

 
 
not_a_commie
Guest
Posts: n/a
 
      11th Jan 2007
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?

 
Reply With Quote
 
 
 
 
Ben Voigt
Guest
Posts: n/a
 
      11th Jan 2007

"not_a_commie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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<S> throughout your
generic class

> 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<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?
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual PC Additions Additions Johann Microsoft Access 7 5th Feb 2010 12:23 AM
dang dang dang checkboxlist Mr. SweatyFinger Microsoft ASP .NET 5 3rd Jan 2007 09:31 PM
Dang it. =?Utf-8?B?TkNfU3Vl?= Microsoft Access Forms 3 26th Jan 2006 10:09 AM
Dang, that's loud! Michael Chiu DIY PC 5 3rd May 2004 01:09 AM
I can't get rid of the dang sound! Hannah Windows XP Customization 2 17th Apr 2004 07:56 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:48 PM.