开发者

How to overload Lua string subscript operator?

This:

debug.getmetatable("").__index = function (s,开发者_StackOverflow i) return s:sub(i, i) end

and this:

debug.getmetatable("").__index = _proc_lua_read

does not work.


Try

debug.getmetatable("").__index = function (s, i) return string.sub(s,i,i) end

Note that by redefining __index for strings in that way, you lose the ability to call methods on strings: note how the code does not call s:sub. For a better solution that avoids that, see http://lua-users.org/lists/lua-l/2007-11/msg00619.html . Or set __call instead:

getmetatable("").__call = string.sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜