Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly return all the possible overloads if no overload is an exact…
Browse files Browse the repository at this point in the history
… match.

Fixes dynamicexpresso#159
metoule committed Mar 7, 2022
1 parent 5b185e9 commit 94f2e0c
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
@@ -1952,9 +1952,14 @@ private static MethodData[] FindBestMethod(IEnumerable<MethodData> methods, Expr
ToArray();
if (applicable.Length > 1)
{
return applicable.
Where(m => applicable.All(n => m == n || MethodHasPriority(args, m, n))).
ToArray();
var bestCandidates = applicable
.Where(m => applicable.All(n => m == n || MethodHasPriority(args, m, n)))
.ToArray();

// bestCandidates.Length == 0 means that no applicable method has priority
// we don't return bestCandidates to prevent callers from thinking no method was found
if (bestCandidates.Length > 0)
return bestCandidates;
}

return applicable;
16 changes: 16 additions & 0 deletions test/DynamicExpresso.UnitTest/GithubIssues.cs
Original file line number Diff line number Diff line change
@@ -173,6 +173,22 @@ public void GitHub_Issue_148()
Assert.AreEqual(2, target.Eval("SubArray(arr1, 1, 1).First()"));
}

[Test]
public void GitHub_Issue_159_ambiguous_call()
{
Func<double?, int> f1 = d => 1;
Func<string, int> f2 = o => 2;

var interpreter = new Interpreter();
interpreter.SetFunction("f", f1);
interpreter.SetFunction("f", f2);

// we should properly throw an ambiguous invocation exception (multiple matching overloads found)
// and not an Argument list incompatible with delegate expression (no matching overload found)
var exc = Assert.Throws<ParseException>(() => interpreter.Eval("f(null)"));
StringAssert.StartsWith("Ambiguous invocation of delegate (multiple overloads found)", exc.Message);
}


#if NETCOREAPP2_1_OR_GREATER

0 comments on commit 94f2e0c

Please sign in to comment.