moonsharp
moonsharp copied to clipboard
TableTestReverseSafer example does not work
TableTestReverseSafer example does not work. This returns 0 instead of 55:
public double TableTestReverseSafer()
{
string scriptCode = "return dosum { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }";
Script script = new Script();
script.Globals["dosum"] = (Func<List<object>, int>)(l => l.OfType<int>().Sum());
DynValue res = script.DoString(scriptCode);
return res.Number;
}
Windows 7 .NET 4.6.1 MoonSharp 1.8.0.0
MoonSharp supports Lua 5.2 where every number is a double (if it had supported 5.3 it wouldn't have worked anyway, because the number would have been longs, in any case).
You declare a List<object>
and provide a table full of numbers, which gets converted to a list full of doubles. OfType<int>
filters away every item, returning an empty list and Sum then returns 0.
Either change to OfType<double>()
, to OfType<double>().Cast<int>().Sum()
or List<object>
to List<int>
.
Great point, thank you!
Please correct the online sample code. My code is directly from this example: http://www.moonsharp.org/callback.html -- TableTestReverseSafer()
Oh thanks, wrong example, will fix.