lua-users home
lua-l archive

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


Lua forces copy semantics for simple value types (nil, boolean, number,
string, light userdata) and reference semantics for complex types (function,
table, userdata).

I have a colleague using Lua in a project that has a strong need for passing
parameters by reference (problem set is the multivariate optimization of a
complex system).  However, since Lua is incapable of this construct he is
forced to pass parameters by name (or to add a level of indirection to every
variable that he might ever wish to pass to a function by reference).

Basically passing a value by reference (other than a
table/function/userdata) requires one of the following (AFAIK).


function SomeFunction(Tbl, Key) do
   Tbl[Key] = Tbl[Key] + 2
end

...or...

function SomeFunction(ParamA) do
   Param[1] = Param[1] + 2	-- Note that the ONLY reason for
end                           -- ParamA to be a table is so that
                              -- the 1 and only value it contains
                              -- can be changed



1. Does anyone know of any pure Lua way of accomplishing "passing by
reference" that is more elegant and maintainable than the above methods?



2. Does anyone have a feel for how hard it would be to modify the Lua
sources to overcome this limitation of Lua?



3. Anyone have a good suggestion for a good Lua replacement for those that
mostly like Lua, but come across "poor fits" in certain projects?




=====================================================
Virgil Smith               EMAIL: Virgil@Nomadics.com
Nomadics Inc               HTTP:  www.Nomadics.com
1024 S. Innovation Way     PHONE: 405-372-9535
Stillwater, OK 74074, USA  FAX:   405-372-9537
=====================================================