lua-users home
lua-l archive

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


One thing that seems asymmetrical to me in Lua is that functions may take
either a fixed or variable number of arguments, while the return values are
always variable.

Pondering that... I see a solution that would potentially add a little more
clarity to functions while retaining compatibility with exisiting code.

Consider extending the function definition to something like:
    function f(a,b,... -> c,d,...)
        c, d = a+b, a-b -- return the 1st and 2nd results (required).
        return a*b, a/b -- return the 3rd and 4th results (optional).
    end

"return" is, effectively, just an assignment to the variable (optional)
portion of the return value.

Exisiting function definitions, eg:
    function f(a,b,...)
are simply shorthand for:
    function f(a,b,...->...)
and so remain compatible.


The key advantage to adding this feature is that functions can become more
clearer to the reader. If a function is designed only to take two fixed
arguments and return a single result (like "add") this can be shown
clearly... and accidentally returning the wrong number of results would be a
syntax error (or more likely just trimmed?). Eg:
    function add(a,b->c)
      c = a+b
    end

----

There are actually three types of 'assignment' : (1) parameter passing, (2)
return value passing, and (3) variable assignment.

With (1) & (2) more consistent we might as well round out (3). This is
enhanced to allow:
    a, b, ... = f()

This simply assigns "arg" just like in a parameter assignment.

*cheers*
Peter Hill.

PS: I haven't looked through Lua5.0 yet, so appologies if this is already
done.