Overload resolution issue for Generic type with implicit operator #2787
-
In the example following, I have a method public static Option<string> MapOpt(Option<int> a){...} which can't directly pass as a parameter to my own version extension method Select. Func<Option<int>, Option<string>> mapOpt = MapOpt; I believe this is an issue because the method should be able to pass as Func parameter as expected. using System;
using System.Collections.Generic;
using System.Linq;
using ClassLibrary2;
using ClassLibrary3;
namespace ClassLibrary2
{
public class Test
{
private static void Call()
{
IEnumerable<int> l = new List<int>() { };
//Linq extension
var r1 = l.Select(Map);
Func<Option<int>, Option<string>> mapOpt = MapOpt;
//Error CS0121 The call is ambiguous between the following methods or properties
var r2 = l.Select(MapOpt);
//Compiled
var r3 = l.Select(mapOpt);
}
public static Option<string> MapOpt(Option<int> a)
{
throw new Exception();
}
public static string Map(int a)
{
return "";
}
}
public class Option<T>
{
public static implicit operator Option<T>(T value)
{
throw new Exception();
}
}
}
namespace ClassLibrary3
{
public static class Ext
{
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<Option<TSource>, Option<TResult>> selector)
{
return null;
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
@ghostnguyen Let me know if this compiles: var r2 = l.Select(x => MapOpt(x)); |
Beta Was this translation helpful? Give feedback.
-
No, it is not. |
Beta Was this translation helpful? Give feedback.
-
This is a rather niche error around how method groups get picked, but you can fix it by moving the using ClassLibrary3;
namespace ClassLibrary2
{
...
} to: namespace ClassLibrary2
{
using ClassLibrary3;
...
} Either that, or add explicit type parameters to your call: var r2 = l.Select<int, string>(MapOpt); |
Beta Was this translation helpful? Give feedback.
-
Yep, I can do work around with an explicit cast. But I think it's an issue of method group selection. Isn't it? |
Beta Was this translation helpful? Give feedback.
-
Verify fixed in .Net 8 |
Beta Was this translation helpful? Give feedback.
Verify fixed in .Net 8