CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Wednesday, October 11, 2006

Constraints in C# vNext + 1

I was reading a post on Troy Magennis’ blog today about his set of LINQ extension methods that he is creating and something struck me about the notation he was using to describe some of his methods.

Example: public static [numeric type] Min(this IEnumerable<[numeric type]> source, int defaultValue)

The interesting bit is “[numeric type]”. It got me thinking about some of the things that we loose without having a full power constraint system in C# 2.0 around what types you accept and reject. I’d actually like the ability to constrain which types are accepted based on what methods they implement.

I wonder what the syntax would need to look like to be that expressive - and how tricky would it be to do at compile time and at runtime? It would probably also require a level of duck-typing support in C#, which is something that VB vNext will have (hopefully it hasn’t been cut).

Post a Comment