lua-users home
lua-l archive

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


on 6/14/04 8:39 AM, Virgil Smith at Virgil@Nomadics.com wrote:

> 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).

Not implemented in Java. Not in Lisp. Not in Smalltalk...

But really, the answer is multiple-value return. If you need to change
multiple values, then you pass them in and receive them back. That's how you
get the same thing as Pascal VAR parameters and C++ references. It gets a
little ugly if one is modifying an entry within a table rather than a
variable, but it's quite doable. For example, it's a little crude and
possibly more error prone to have to write:

    table[ index ] = adjust( table[ index ], other_param )

Rather than:

    adjust( table[ index ], other_param )

But you also get code that's easier to understand with respect to exceptions
and side-effects.

Perhaps with a more detailed example in some other language, the list can
better analyze what your friend needs to do.

Mark

P.S. As a language that addresses these things via syntactic sugar, perhaps
Lua needs something like:

    let x be table[ index ] in
        x = adjust( x, other_param )
    end

This introduces an alternative name for an assignment path. I would argue
that it should probably also capture the values of table and index so that x
continues to refer to the same thing. In other words, it translates to:

    do
        local _temp1, _temp2 = table, index
        _temp1[ _temp2 ] = adjust( _temp1[ _temp2 ], other_param )
    end