moonsharp icon indicating copy to clipboard operation
moonsharp copied to clipboard

TableTestReverseSafer example does not work

Open rmcrackan opened this issue 8 years ago • 3 comments

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

rmcrackan avatar Oct 13 '16 13:10 rmcrackan

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>.

xanathar avatar Oct 13 '16 14:10 xanathar

Great point, thank you!

Please correct the online sample code. My code is directly from this example: http://www.moonsharp.org/callback.html -- TableTestReverseSafer()

rmcrackan avatar Oct 13 '16 14:10 rmcrackan

Oh thanks, wrong example, will fix.

xanathar avatar Oct 13 '16 14:10 xanathar