gpt4 book ai didi

string - 如何重载Lua字符串下标运算符?

转载 作者:行者123 更新时间:2023-12-01 11:57:21 26 4
gpt4 key购买 nike

这个:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com