lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 20 October 2014 12:53, Paco Zamora Martínez <pakozm@gmail.com> wrote:
Thinking about the problem, comes to my head the reason for the lack of operator [] in Lua strings. IMO, it is related with the same problem I'm facing here.

It's not hard to change the string metatable to do this,
at least for an accessor; a __newindex doesn't make sense, as strings are immutable.

local stringlib = string
local string_mt = debug.getmetatable("")
string_mt.__index = function(s,k) if type(k) == "number" then return stringlib.sub(s, k,k) else return stringlib[k] end end

print(("asd")[2])