Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infer second generic parameter based on the expression argument of the function

Tags:

c#

generics

This question is an extension of Partial type inference If partial type inference is not possible, could anyone please explain how OrderByDescending extension method is working? Please note that when I call OrderByDescending(m=>m.DateProp), I am not asked to provide Type parameter.

static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>(
   this IQueryable<TSource> source, 
   Expression<Func<TSource, TKey>> keySelector);
like image 217
Ishwor Avatar asked Dec 25 '22 00:12

Ishwor


1 Answers

could anyone please explain how OrderByDescending extension method is working?

I surely can.

static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>
(
  this IQueryable<TSource> source, 
  Expression<Func<TSource, TKey>> keySelector);

When you call foo.OrderByDescending( x => bar ) type inference proceeds as follows.

  • First a set of bounds for TSource is inferred by examining foo.
  • Then we ask "are there any more inferences we can make without knowing TSource?" The answer is no, so we fix the value of TSource to the best member of the bound set. Call it S.
  • Then we ask "given the value of TSource is S, can we infer the value of TKey? Yes. We apply the type value S to x, and then infer the type of expression bar in an environment where x is of type S.
  • The type of bar gives us the type for TKey and we're done.

If partial type inference is not possible,...

Partial type inference is not impossible. Rather, it's not implemented. It's possible to add the feature to C#; no one has done it.

Regardless, whether or not inference from a partial set of bounds is implemented or possible, there's no difficulty in doing a complete inference of all the types in a call to OrderByDescending. Obviously we designed the algorithm specifically to be able to handle OrderByDescending, Join and so on.

like image 150
Eric Lippert Avatar answered Feb 16 '23 02:02

Eric Lippert