The following won't compile. It feels like it should. I'm wondering if there is a way around the problem. If possible, I'd prefer to avoid requiring anything to descend from class.
public static Func<DP, R> CastDomain<D, R, DP>(this Func<D, R> function) {
return (DP dp) => {
if (dp is D) {
D d = (D)dp; // Compile error: cannot convert DP to D. But that's crazy as we are inside if (dp is D) {
return function(d);
} else {
// handle the error . . .
}
};
}