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:45:38PM -0400, Diego Nehab wrote:
> >>= s["len"]
> >function: 0x806be90
> >>= string.len
> >function: 0x806be90
> 
> So you can call "foo":len() and get string.len("foo")...

Ok, I think I start to see. So unwrapping the layers of syntactic pastry:

	"foo":len()

is

	"foo".len("foo")

is

	"foo"["len"]("foo")

is

	getmetatable("foo")["__index"]["len"]("foo")

Wouldn't it be useful to do for numbers, too?

> = math.floor(1.1)
1
> n = 1.1
> n:floor()
stdin:1: attempt to index global 'n' (a number value)
> v = { __index = math }
> setmetatable(n, v)
stdin:1: bad argument #1 to 'setmetatable' (table expected, got number)

getmetatable() works on any object, but setmetatable() only works on
tables?

Sam