Search code examples
luasyntax-errorlua-table

Already put } but the error still says that } is expected?


I took this from a tutorial, when i attempted to run it, a confusing error appears

local t1 = {"hi", true}
local t2 = {79, "bye", false}
local mt = {
    __concat = function(v1, v2)
        local output = {}
        for i, v in pairs(v1) do
            table.insert(output, v)
        end
        for i, v in pairs(v2) do
            table.insert(output, v)
        end
    end
    return output
    }
setmetatable(t1, mt)
setmetatable(t2, mt) --they gotta have it both as well

local t3 = t1..t2 --we merged t1 and t2 together, as you can see you can get creative
print(unpack(t3)) --t3 contains all of t1 and t2's members

The error is : File:13: '}' expected (to close '{' at line 3) near 'return'


Solution

  • Your problem: you're taking the error message too literally without taking the time to understand it.

    Lua isn't just randomly expecting a } exactly on line 13, it's expecting something that makes sense in a table literal and finds a return instead, so it defaults to tell you the first thing it can think of that would make sense, aka. closing the table with a } first.

    The real problem, is that the return is outside of the function, so Lua doesn't know what to make of it. It needs to go above the end instead.

    Generally speaking: When Lua tells you Expected something on line X, you should just read it as I found something weird on line X and have no idea what you mean. A better error message would have been encountered return in table literal I guess.