codingseb / ExpressionEvaluator

A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts
MIT License
595 stars 99 forks source link

Linq Select throws exception (ValueTuple?) issue? #139

Open yggdrasil-tynor opened 2 years ago

yggdrasil-tynor commented 2 years ago
context.AddVariable("list1", new int[] { 1, 2, 3, 8 });
context.AddVariable("list2", new int[] { 4, 5, 6, 7 });
var test = context.Evaluate("list1.Cast<int>().Select<int, int>((item, i) => item - list1[i]).Cast<int>().Sum()");

throws:

ExpressionEvaluatorSyntaxErrorException : The call of the method "Sum" on type [System.Linq.Enumerable+SelectArrayIterator`2[System.Int32,System.Int32]] generate this error : Index was outside the bounds of the array.

With Zip or EquiZip it works fine:

list1.Cast<int>().EquiZip<int, int, int>(list2, (l1, l2) => l1 + l2).Cast<int>().Sum()"

What is wrong with select?

yggdrasil-tynor commented 2 years ago

EDIT: Seems like it's an issue when using ValueTuple.... Maybe this is not supported?

Here is a simple .NET fiddle example which shows the unexpected behaviour:

yggdrasil-tynor commented 2 years ago

This line breaks it: https://github.com/codingseb/ExpressionEvaluator/blob/365551a87cda0de63a18d603f257354460b1f8c3/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs#L3425

For some reason the internalDelegate args contains 1 item, and should contain 2 in this case... https://github.com/codingseb/ExpressionEvaluator/blob/365551a87cda0de63a18d603f257354460b1f8c3/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs#L3504-L3516

I guess this overload requires some special handling?

yggdrasil-tynor commented 2 years ago

FYI this is still an issue in 1.5.0-alpha releases