[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Making a string indexing metamethod
- From: "Reuben Thomas" <rrt@...>
- Date: Wed, 08 Nov 2006 03:04:44 -0000
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/