lua-users home
lua-l archive

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


...
 
> After having given this some more thought, I have realized 
> that part of the
> issue is that "return" actually has two behaviors that are 
> sandwitched into
> one. This was probably a good idea in single-return-value 
> languages like C,
> but in lua I don't think it necessarily makes sense:
> 
> 1) Push one or more values onto a "return stack"
> 2) exit the current thread of execution and return the stack 
> to its caller
> 
> Now, why couldn't we split these two things up. Just have a 
> side-effect of
> running code in the eval() context that each value is pushed onto an
> implicit return stack. Explicit returns still operate as 
> usual. But if the
> eval() terminates before returning, then the implicit return stack is
> automatically returned as a result.
> 
> Anything wrong with that approach?
> 
> Eric
> 

I believe that yes.
First, the return statement is symmetrical to a function call, where you
give the values in the order they should be and send them at once to where
they should go.
Second, if we could push the values into a "return stack" (or in a
"parameter stack"), the complexity (the number of different paths that the
interpreter may follow from the beginning of the chunk until its end) in
determining which values would be returned (or passed as parameters to a
function in a function call) for each return statement, would be increased
from 1 (with the current approach) to the number of paths.
So, I guess bad written code (code that is hard to understand / too much
complex) would get worse.

Of course your approach would bring good consequences if programmers used a
consistent style to deal with it (a style that would make it easy to see
what values will be returned), but even good programmers might feel
attempted to use it and, sometimes, forget to document all the return
possibilities (or parameter possibilities).

Note that a stack may be easily implemented using tinsert and tremove, and,
in Lua 4.0, you can do something like "return stack2returnval(return_stack)"

[]'s
Luiz