[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Multiple return value in __index metamethod
- From: Lavergne Thomas <thomas.lavergne@...>
- Date: Tue, 10 Jul 2007 20:37:57 +0200
On Tue, Jul 10, 2007 at 12:54:37PM -0500, Rici Lake wrote:
> __index is triggered by the use of an index operation in Lua, including
> t[3], t.foo, t:meth()
>
> Like the arithmetic operators, these operations are adjusted to
> a single value; in the compiled VM code, they compile to a single
> operation. Allowing multiple returns would be a complication to
> the VM design, and would likely be considerable overhead for a
> fairly rare use case.
Ok, so there is a really good reason I've not seen. Too bad, but I will
have to live with this.
> The number of return values consumed is determined by the caller,
> not the function called. So the description is technically correct.
I've missed this also, all my apologies. But perhaps documenting this
could be less confusing (but is it necessary to document all corner use
like this ...)
> >In my case I have different possibility do handle this. I can make my
> >__index methods return a table of these values, but this have the
> >ovrhead to create a lot of very small tables destroyed almost imediatly
> >and so trigger the gc a lot more often, and I must modify all the
> >existing code.
>
> Why not just use classic accessor functions? That is:
> obj:values()
> instead of
> obj.values
>
> It seems easier than the other possibilities, and has the advantage
> of working :)
>
Yes it works but it is less elegant.
at start I have :
value = object[idx]
it was simple and elegant. I've hopped for this :
value, delta = object[idx]
but it's impossible.
So I have two choices. First a method for the second value :
value = object[idx]
delta = object:delta(idx)
But this imply to make the computation two times. It isn't so expensive
but it isn't negligible (not sure of this is the correct word)
Or I can make a method that return both, but it's not very elegant to
have two way to retrieve the same value.
For the moment I've choose this way. Add a method who return both value
and make indexing with an integer a shortcut for it who return only the
first value.
The problem is that I'm sad because it is the first time I cannot make
the elegant design I have in mind with Lua. Lua is not perfect, just
close to be perfect ;-)
Tom
--
Thomas Lavergne "Le vrai rêveur est celui qui rêve
de l'impossible." (Elsa Triolet)
thomas.lavergne@reveurs.org http://reveurs.org