lua-users home
lua-l archive

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

I've been thinking, which isn't a good sign...

On another thread I mentioned that I liked the colon notation and wished that it could be applied to all types; PA disagreed, using dental care for his reasoning. :)

Mike Pall pointed me to the code that handles it along with a suggestion on how to make it work. I will probably play around with it at some point (I'm already spending time away from work I *should* be doing!). For now I just want to discuss it.

I think it would be nice to extend the use of '.' and ':' to all types. I'd like to be able to do something like this:

> s='abcd'
> s:print()
> print(s:len())
> c=coroutine.create(function() print('hey!') end)
> c:resume()
> n=6
> n:print()

The last one doesn't help much but I think the other two help with readability - and typing.

I was thinking that each non-table, non-nil type - 'number', 'string', 'thread', 'boolean' - would have its own table with a metatable attached with __index set to that table. This would allow the VM to easily (and quickly) push the table associated with the type onto the stack before attempting to index a non-table type. 'nil' would still result in an error.

In the examples above, the ones that are of the form x:print() would require the use of the global table if the type table did not resolve the name. I'm not so worried about supporting that as I am about things like s:len() mapping to string.len(s). In the 'print' case it may be better for an error to be raised. A developer could always set string.print = print and the system above would work.

I like the idea, and I'm wondering what others think. Am I the only one who loves the colon notation and wishes that it applied to all the built-in types?

By the way, why do we call the library 'coroutine' and the type 'thread', but with string we use 'string' and 'string'? I suspect it is to avoid the name space collisions for 'thread' but to keep the type name shorter than 'coroutine'. Of course the name space argument could certainly be used for 'string'. Unfortunate, since it makes this idea harder to implement.

Thanks to the creators of Lua. It is a thing of beauty, even with these little 'flaws'. :)


Doug Rogers - ICI - V:703.893.2007x220