lua-users home
lua-l archive

Making a string indexing metamethod

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


To allow string methods to continue to work, you can't just override __index. After some thought, I came up with this:
-- @func __index: Give strings a subscription operator
-- @param s: string
-- @param n: index
-- @returns
-- @param s_: string.sub (s, n, n)
local oldmeta = getmetatable("").__index
getmetatable("").__index =
 function (s, n)
 if type (n) == "number" then
 return sub (s, n, n)
 else
 return oldmeta[n]
 end
 end
What are the downsides?
--
http://rrt.sc3d.org/

AltStyle によって変換されたページ (->オリジナル) /