本文介绍了Moonsharp对(...)引发异常“错误的参数#1到'next'(期望的表,有字符串)".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么会这样.我正在使用Moonsharp在我的应用程序中运行LUA脚本,我创建了一个LUA函数IN(v,...),并且希望对...参数进行配对.

I don't get the point why this is happening.I am using Moonsharp to run LUA scripts in my application an I created a LUA function IN(v, ...) and I'd like to itterate over the ... parameter with pairs.

IN('param1', 'param2', 'param1') -- expected it to return true
function IN(v, ...)
    local args = ...
    local res = true
    for i, v in pairs(args) do
        if valueIn == v then
            res = true
            break
        end
    end
    return res
end

如果被调用,我将收到以下异常:

If it gets called I recieve the folowing exception:

所以我决定检查...变量中是否有字符串而不是表格.

So I decided to check if there is a string instead of a Table in my ... variable.

function args(v, ...)
    return ...
end

C#中的返回值是具有'param2'和'param1'的2个值的元组,所以它应该与pairs或ipairs一起使用,不是吗?

The return value in C# is a Tuple of 2 values with 'param2' and 'param1', so it should work with pairs or ipairs, shouldn't it?

谢谢.

推荐答案

像您的示例一样使用此定义:

Using this definition like in your example:

function test(...)
  local arg = ...
end

并致电

test(1,2,3)

将导致

local arg = 1, 2, 3

哪个当然只会为arg分配1.其余的省略.

which of course only assigns 1 to arg. The rest is omitted.

但是当表构造函数将...作为输入时,您可能会写

But as the table constructor takes ... as input you may write

local arg = {...}或,然后愉快地遍历新表arg....不是lua刚刚告诉您的表.因此,您无法遍历...

local arg = {...} or and then happily iterate over your new table arg.... is not a table as lua just told you. Hence you cannot iterate over ...

或者也可以使用本地arg = table.pack(...).

在Lua 5.1中已更改了vararg系统,以防您好奇 https://www.lua.org/manual/5.1/manual.html# 7.1

The vararg system has been changed in Lua 5.1, in case you're curioushttps://www.lua.org/manual/5.1/manual.html#7.1

在此之前,您可以做类似的事情

Befor you could do something like

function test(...)

  for k,v in pairs(arg) do
    print("I'm a generic for loop yeah!!!")
  end

end

因此local arg = {...}不是必需的.

这篇关于Moonsharp对(...)引发异常“错误的参数#1到'next'(期望的表,有字符串)".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 02:55