Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser.MethodHasPriority regression #191

Closed
waclaw66 opened this issue Nov 22, 2021 · 5 comments
Closed

Parser.MethodHasPriority regression #191

waclaw66 opened this issue Nov 22, 2021 · 5 comments

Comments

@waclaw66
Copy link

Method Parser.MethodHasPriority causes a regression in my tests. None of two possible methods is chosen.

methods:

0: {System.Collections.Generic.IEnumerable`1[System.Object] Select[TSource] System.Collections.Generic.IEnumerable`1[TSource], System.String)}
1: {System.Collections.Generic.IEnumerable`1[System.Object] Select(System.Collections.IEnumerable, System.String)}

and args passed:

0: {System.Collections.Generic.List`1[System.Object]}
1: {System.String}

See following test example...

namespace EvalTest
{
    using System.Collections;
    using System.Collections.Generic;
    using DynamicExpresso;
    using NUnit.Framework;

    [TestFixture]
    public class EvalTest
    {
        private Interpreter interpreter = new Interpreter();

        [OneTimeSetUp]
        public void Setup()
        {
            this.interpreter.Reference(typeof(Utils));
        }

        [Test]
        public void EvaluateTest()
        {
            var list = new[] { 1, 2, 3 };
            Assert.DoesNotThrow(() => { this.Evaluate("Utils.Array(1, 2, 3)"); });
            Assert.DoesNotThrow(() => { this.Evaluate("Utils.Select(Utils.Array(\"a\", \"b\"), \"x+x\")"); });
        }

        private object Evaluate(string expression, params Parameter[] parameters)
        {
            return this.interpreter.Parse(expression, parameters).Invoke(parameters);
        }
    }

    public class Utils
    {
        public static List<T> Array<T>(IEnumerable<T> collection)
        {
            return new List<T>(collection);
        }

        public static List<dynamic> Array(params dynamic[] array)
        {
            return Array((IEnumerable<dynamic>)array);
        }

        public static IEnumerable<dynamic> Select<TSource>(IEnumerable<TSource> collection, string expression)
        {
            return new List<dynamic>();
        }

        public static IEnumerable<dynamic> Select(IEnumerable collection, string expression)
        {
            return new List<dynamic>();
        }
    }
}
@metoule
Copy link
Contributor

metoule commented Nov 22, 2021

The regression occurred in 2.9.0, so it's linked to PR #140 that adds support of lambda expressions.

@davideicardi
Copy link
Member

@metoule PR #192 is not enough to fix the bug? What is the case that is not yet covered?

@waclaw66
Copy link
Author

@davideicardi The case with Utils.Select described above. Utils.Array was ok previously.

@metoule
Copy link
Contributor

metoule commented Nov 23, 2021

@davideicardi no, it doesn't solve the issue; I still have to fix the MethodHasPriority bug. It's a bit tricky because I have to handle both the fix for this bug, while not breaking the lambda expression support.

PR #192 is not really needed to fix this bug, but I found it weird to use dynamic instead of generics, which is how I realized that it was not possible to use generics with params array.

@waclaw66 Utils.Array was indeed ok, but you'll now be able to use generics to have a more strongly typed List:

private static class Utils
{
  public static List<T> Array<T>(params T[] array)
  {
    return new List<T>(array);
  }
}

@metoule
Copy link
Contributor

metoule commented Nov 23, 2021

Note that the issue occurs because the Select method has two overloads that clash with the current resolution; removing either overload should solve the issue.

metoule added a commit to metoule/DynamicExpresso that referenced this issue Nov 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants