lua-users home
lua-l archive

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

Hello Brent,

Taking this off list...

BA> I presume you'll tell me I'm mistaken, but man I gotta tell you, the
BA> explanation of why passing by reference in lua, is not passing by
BA> reference - is altogether too esoteric for me.  :)

Perhaps you are using the word "reference" differently than others.

Let's say that the variable R contains a table T. I'll "draw" this as:

R ---> T

If we pass R to a function, what is passed?

There are several options.

Lua (and C if T was an array) pass the arrow in the picture above.
I suspect you are calling this a "reference." However, in "call by
reference" what is passed is the whole shebang (R ---> T) so the
function can modify R, for example, making it point to a different
thing, e.g., after the call we could have

R ---> Q      (Q != T)

In call by value languages, the function cannot modify what R points
to (it can modify the thing R points to, but R will still point to

So, most programmers call what Lua does "call by value" because what
is passed is the value of R (i.e., the arrow above) and nothing in the
function can change R even though it can change T.

Other calling options are to copy the whole table, or just pass the
name "R" to the function. This makes four options so far. I call these
call by copy       -- everything in the caller is immutable
call by value      -- variables in the caller are immutable
call by reference  -- nothing in the caller (passed to function) is immutable
call by name       -- nothing in the caller (passed to function) is
immutable even after the function returns, and with weird scope