Hi. The attached patch to mcs seems to fix bug 357047 (which I also
filed). The issue is that the compiler can't tell that:

  enumerable.Average(o => op on o that returns a double);

or

  enumerable.Average(delegate(T o) { return (op on o that returns a
double); });

Means Average<T>(IEnumerable<T>,Func<T,double>) or
Average<T>(IEnumerable<T>,Func<T,double?>). This means that you get an
error that the method call is ambiguous.

The patch works by inferring the return type of the argument, then
testing if that return value is an exact match for the return value of
the Func parameter. If so, it breaks ties with the exact match.

This is the first hacking I've done on mcs, so I'm not sure about how
correct this is. Comments are welcome.

Thanks.
Index: ecore.cs
===================================================================
--- ecore.cs	(revision 94270)
+++ ecore.cs	(working copy)
@@ -3303,7 +3303,46 @@
 				if (!q.IsValueType && p == TypeManager.object_type)
 					return q;
 			}
-                                
+
+#if NET_2_0
+			if (argument_expr is AnonymousMethodExpression)
+			{
+				AnonymousMethodExpression amex =
+					argument_expr as AnonymousMethodExpression;
+				try
+				{
+					Type rt_p = amex.InferReturnType(ec, null, p);
+					Report.Debug(256, "rt_p", rt_p);
+					Type[] t_p = p.GetGenericArguments();
+					if (rt_p == t_p[t_p.Length - 1])
+					{
+						Report.Debug(256, "better return value match");
+						return p;
+					}
+				}
+				catch (Exception x)
+				{
+					Report.Debug(256, "exception", x);
+				}
+
+				try
+				{
+					Type rt_q = amex.InferReturnType(ec, null, q);
+					Report.Debug(256, "rt_q", rt_q);
+					Type[] t_q = q.GetGenericArguments();
+					if (rt_q == t_q[t_q.Length - 1])
+					{
+						Report.Debug(256, "better return value match");
+						return q;
+					}
+				}
+				catch (Exception x)
+				{
+					Report.Debug(256, "exception", x);
+				}
+			}
+#endif
+
 			if (argument_type == p)
 				return p;
 
_______________________________________________
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list

Reply via email to