lua-users home
lua-l archive

Re: string[], doesn't exist?

[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:
Thanks, that works as I hoped [] might, and is very educational.
It still leaves my wondering _why_, since indexing a string by a number
seems to effectively do nothing. Is the desire to do this considered to
be an end-user decision, when building their lua application?
> s="lua"
> print(s[2])
--> nil
What does it do? Playing around, it looks like I can do
> = s["len"]
function: 0x806be90
> = string.len
function: 0x806be90
So, it looks like
 getmetatable("").__index == string
for strings, but is nil for other builtins:
	> = getmetatable(5)
	nil
	> = getmetatable{}
	nil
	> = getmetatable(io.input())
	table: 0x806aae0
	> = io
	table: 0x806ad78
	>
Its not clear to me what the function of this is, why this is useful.
Thanks for your patience,
Sam

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