【问题标题】:Lua - 'end' expected (to close 'function' near <eof>), don't see missing 'end'Lua - 预期“结束”(关闭 <eof> 附近的“功能”),看不到缺少“结束”
【发布时间】:2016-12-03 16:35:34
【问题描述】:

我收到一条错误消息,提示缺少“end”,但我查看了整个代码但没有看到。

local Grid = {}
Grid.__index = Grid
function Grid.new(w, h) do
    t = {}
    setmetatable(t,Grid)
    for i=1,w do
        t[i] = {}
        for j=1,h do
            t[i][j] = {i, j, nil}
        end
    end
    return t
end
Grid.__call = Grid.new
return Grid

这是错误:

lua: grid.lua:15: 'end' expected (to close 'function' at line 3) near <eof>

【问题讨论】:

    标签: lua


    【解决方案1】:

    do 放在function 行上。您有一个与function 匹配的end,但没有一个与(不必要的)do 匹配。 (实际上编译器认为enddo 匹配,然后在没有看到函数的end 时抱怨。)

    函数体的语法大概是

    function name( paramsopt ) block end

    (这过于简单,详情请参阅the Lua reference。)

    do 不是必需的。如果存在,则不是函数体语法的一部分,必须与对应的end 匹配。

    【讨论】:

    • 谢谢!我忘了 Lua 函数不需要do
    • @Alchemist 不要忘记点击旁边的 ✅ 将答案标记为“正确”!
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 2020-02-16
    • 2016-05-07
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    相关资源
    最近更新 更多