lua-users home
lua-l archive

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


Nice! :-)
But I can't do the same in my code, the instance object metamethod __index is forced to be a table (some functions expect that, which will need a "hard" code rewritting)

2014-10-20 19:24 GMT+02:00 Daurnimator <quae@daurnimator.com>:
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])






--
Pako ZM :)
http://cafre.dsic.upv.es:8080/~pako/