Moonsharp对(...)引发异常“错误的参数#1到'next'(期望的表,有字符串)". [英] Moonsharp pairs(...) raises exception "bad argument #1 to 'next' (table expected, got string)"

查看:236
本文介绍了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:

"MoonSharp.Interpreter.ScriptRuntimeException" 错误的参数#1到下一个"(期望表,有字符串)

"MoonSharp.Interpreter.ScriptRuntimeException" bad argument #1 to 'next' (table expected, got string)

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

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 curious https://www.lua.org/manual/5.1/manual.html#7.1

vararg系统从带有表的伪参数arg更改为 带有vararg表达式的额外参数. (请参阅编译时 luaconf.h中的选项LUA_COMPAT_VARARG.)

The vararg system changed from the pseudo-argument arg with a table with the extra arguments to the vararg expression. (See compile-time option LUA_COMPAT_VARARG in luaconf.h.)

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

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'(期望的表,有字符串)".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆