Search code examples
luasyntax-errorscite

Can you please tell me what's wrong with this code?


Here's some Lua code I just wrote in SCIte, and I don't know what's wrong with it exactly, so could someone explain to me what I did wrong and how to fix it?

t = setmetatable({},{
__newindex = function(t, key)
if key == false then
  return( "False cannot exist in table")
  key = nil
  end
if key == __string then
  table.concat[table, key]
else
  table[key] = nil
  end
if key == nil then
  return  "Tables in this file cannot contain false values."
end
}
)

function Error()
  _,cError = pcall(__index)
end
function Call1()
  error("error in metatable function, '__index'", 1)
end
function Call2()
  Call1()
end

Error()

Call2()

Solution

  • There are many things wrong with it, so many it's almost impossible to amend. However these fixes may help you, I've tried to fix your function here based on what you had before. I suggest you learn more about the language before attempting to create classes with metatables.

    t = setmetatable({},{
        __newindex = function(t, key)
            if key == false then
              return( "False cannot exist in table") -- return values in this function don't make sense
              --key = nil THIS WILL NEVER BE REACHER
            end
            if type(key) == "string" then --check key is a string
              table[key] = {}
            else
              table[key] = nil 
            end
            if key == nil then
              return  "Tables in this file cannot contain false values."
            end
        end
        }
    )
    

    Further

    function Call1()
      error("error in metatable function, '__index'", 1)
    end
    

    Is nonsensical, it will always output an error, i.e:

    error("No error here")
    

    Will produce

    lua: ex.lua:26: No error here