【问题标题】:How to overload Lua string subscript operator?如何重载Lua字符串下标运算符?
【发布时间】:2011-04-16 10:31:41
【问题描述】:

这个:

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

还有这个:

debug.getmetatable("").__index = _proc_lua_read

不工作。

【问题讨论】:

    标签: string lua operator-overloading


    【解决方案1】:

    试试

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多