lua-users home
lua-l archive

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


On Wed, Nov 24, 2010 at 9:13 PM, steve donovan <steve.j.donovan@gmail.com> wrote:
On Wed, Nov 24, 2010 at 12:10 PM, James Rhodes
<jrhodes@roket-enterprises.com> wrote:
> With regards to your last note about accessing characters through s[i], you
> can actually already do this by setting the metatable for all strings; like

OK, so the question would be: how much does this slow down regular
method dispatch like s:sub(i,i)?

No idea, I just got it working in the Lua live demo :P
 
I'm ambivalent about 'global subclassing' because it can be very cool
for small programs but doesn't play nice with other people's code
(especially if they have decided to override the string metatable for
their own purposes ;))

Although not displayed in the code example I posted, you could and probably
should always grab the existing __index metamethod and call that if you can't
or don't want to handle a specific situation (instead of returning string[i]).

That said, I can't see many other uses for __index in the context of string access,
so I doubt you'll see any conflicts.
 
steve d.