开发者

How to overload Lua string subscript operator?

开发者 https://www.devze.com 2023-02-25 01:13 出处:网络
This: debug.getmetatable(\"\").__index = function (s,开发者_StackOverflow i) return s:sub(i, i) end

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

精彩评论

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