这个:
debug.getmetatable("").__index = function (s, i) return s:sub(i, i) end
还有这个:
debug.getmetatable("").__index = _proc_lua_read
不起作用。
最佳答案
尝试
debug.getmetatable("").__index = function (s, i) return string.sub(s,i,i) end
请注意,通过以这种方式为字符串重新定义 __index
,您将失去对字符串调用方法的能力:请注意代码如何不调用 s:sub
。有关避免这种情况的更好解决方案,请参阅 http://lua-users.org/lists/lua-l/2007-11/msg00619.html .或者改为设置 __call
:
getmetatable("").__call = string.sub
关于string - 如何重载Lua字符串下标运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5685920/