lua-users home
lua-l archive

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


Hi,

Doug Rogers wrote:
> >> print(s:len())
> >stdin:1: attempt to index global `s' (a string value)
> >stack traceback:
> >       stdin:1: in main chunk
> >       [C]: ?
> 
> 
> Well, I understand why this is the way it is - the colon notation tries 
> to index the table 's', but 's' is not a table. What I'm looking for is 
> a change in the parser so that it will use the global table (or string, 
> or ...) if 's' is not a table.

The parser doesn't care (except for literals). It's the VM that throws
the error at runtime.

It would be pretty simple to add another case for strings (or any other
object type) in luaT_gettmbyobj() at the end of ltm.c. You'd need to
set up a metatable first (probably in the string library), put it into
the registry (or anchor it at the global state) and return it here.

That's all you need to do, to get the colon notation for string variables
(and maybe some changes to the parser for string literals).

I guess the Lua authors have already considered this possibility
(because it's so simple to implement), but probably rejected it.

My personal opinion: nice to have, but I have no urgent need.

Bye,
     Mike