lua-users home
lua-l archive

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


On Thursday 13 October 2005 13:04, Jose Marin wrote:
> Thinking on a more esay to use than a fast solution,
> how hard would be to create a "String" class in Lua,
> with the operator [] overloaded?

With Lua 5.1, I believe that the 'string' table is the default metatable for 
strings. So:

function string:__index(s, i)
  if (type(i) == "number") then
    return string.substr(s, i, i)
  end
  return nil
end

...might do what you want. (Untested. Never used Lua 5.1.)

-- 
+- David Given --McQ-+ "USER'S MANUAL VERSION 1.0:  The information
|  dg@cowlark.com    | presented in this publication has been carefully
| (dg@tao-group.com) | for reliability." --- anonymous computer hardware
+- www.cowlark.com --+ manual

Attachment: pgpFJq8s30MlP.pgp
Description: PGP signature