Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must I provide explicitly generic parameter types While the compiler should infer the type?

Why must I provide explicitly generic parameter types While the compiler should infer the type?

public static T2 Cast<T1,T2>(this T1 arg) where T2 : class where T1 : class
{
    return arg as T2;
}

Sample Usage:

 objOfTypeT2 = objOfTypeT1.Cast<TypeT1,TypeT2>();


Compared to my desired usage with a more intelligent compiler:

 objOfTypeT2 = objOfTypeT1.Cast<TypeT2>();

or maybe I should be more intelligent :-)

Beware that I provide the return type. I want to not provide the object that I called the function on it, the method is an Extension Method.

like image 813
Jahan Zinedine Avatar asked Dec 18 '10 10:12

Jahan Zinedine


2 Answers

Inference doesn't consider the return type; you can, however, try splitting the generics; for example, you could write code to allow:

.Cast().To<Type2>()

by having (untested; indicative only)

public static CastHelper<T> Cast<T>(this T obj) {
    return new CastHelper<T>(obj);
}
public struct CastHelper<TFrom> {
    private readonly TFrom obj;
    public CastHelper(TFrom obj) { this.obj = obj;}
    public TTo To<TTo>() {
       // your code here
    }
}
like image 77
Marc Gravell Avatar answered Nov 10 '22 08:11

Marc Gravell


The specification limits type parameter inference for generic methods to all or nothing. You can't have partial inference.

The rationale is probably simplifying type inference rules (that are already pretty complex, as they have to take into account overloading rules too).

like image 40
mmx Avatar answered Nov 10 '22 09:11

mmx