lua-users home
lua-l archive

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


Thomas Lavergne:
> Is there any reason for the __len metamethod to reduce only 
> allow returning a single value ?
> The pseudo code in the manual :
>           if h then
>             -- call the handler with the operand
>             return h(op)
> doesn't seems to do this adjustement.
> 
> I actually write a matrix object in lua, and when 
> implementing the __len metamethod, I want to return two 
> values, the number of row and the number of col. But lua 
> remove the second return value.
> 
> Is it ok too remove this limitation, or does this have some 
> not so nice side-effects ?

IIRC I already used multiple return values for the __len metamethod of
some of my userdata, but I may be wrong. As a workaround you could
return a table containing all your dimensions and access them with the .
notation:

local m = newmatrix(3, 4)
assert((#m).cols == 3)
assert((#m).rows == 4)

Also not that if you implemented your matrix in pure Lua using tables,
you cannot override __len metamethods for tables (for speed reasons).