lua-users home
lua-l archive

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

On Wed, Jun 21, 2006 at 01:37:01PM -0300, Luiz Henrique de Figueiredo wrote:
> > why does [] not apply to strings?
> It does, but it does not do what you want. But try this:
>  s="lua"
>  print(s[2])
>  getmetatable("").__index = function (s,i)
> 	 if type(i)=="number" then
> 		 return string.sub(s,i,i)
> 	 else
> 		 return string[i]
> 	 end
>  end
>  print(s[2])

This is pretty useful, so I was thinking [] could be used for bit access
on numbers, too, but:

	getmetatable(0).__index = function (s,i)
			return 13 -- random value to see if this is possible

Doesn't work... I guess strings have metatables, but numbers don't?
Whats going on here?