You need to return a function from the __index handler, not a string:
local function capture(table, key, rest)
return function(...)
local args = {...}
print(string.format("call to %s with key %s and arg[1] %s",
tostring(table), tostring(key),
tostring(args[1])))
end
end
-- call to table: 0x7fef5b40e310 with key foo and arg[1] nil
-- call to table: 0x7fef5b40e310 with key foo and arg[1] bar
You're getting an error because it's trying to call the result, but it's currently the key.