[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: Passing Parameters by Reference
- From: "Virgil Smith" <Virgil@...>
- Date: Mon, 14 Jun 2004 10:39:15 -0500
> Since Lua allows multiple return values,
> you can always just use the idiom:
> a = f(a)
The case at hand is a multivariate optimization problem (and several similar
a = f(a)
is not a valid solution, as it does not allow the creation of a function
that performs the optimization in a general fashion.
Consider the following
function Optimize(f, v, min, max) do
-- f is the function to be optimized (minimized)
-- v is the variable quantity, i.e. the position
-- along the "search" axis
-- min is a lower boundary for v
-- max is an upper boundary for v
Note that f is not passed any arguments by Optimize as Optimize should have
no dependence on the number of parameters upon which f operates nor upon
which parameter is currently being operated upon. So f will operate either
on globals or on member variables of an object.
Anyone who thinks that a function such as Optimize should be re-implemented
for every object(or class) of which a target function "f" is a member needs
to look up the term "generic programming" and buy a book on it.
As I stated before my colleague is currently "passing by name" when passing
by reference is called for. This means that all functions (such as
"Optimize" above) must accept a table/userdata and the NAME of the variable
quantity. This allows an operation such as....
v = v + delta
can be written as...
tbl[v] = tbl[v] + delta
Frankly he's just tired of having to write extra code because Lua is missing
a key feature of almost all popular programming languages (i.e. the ability
to pass by reference).