lua-users home
lua-l archive

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

Alright. Here's what's going on. When you have a table in a variable,
that variable doesn't actually contain the table. It contains a
REFERENCE to the table. On the VM level, this is actually a pointer.
The semantics for a table reference, actually, are pretty much the
same as those of a C pointer (without pointer arithmetic, of course).
The REFERENCE is copied, but the TABLE itself is not copied.

MAN!!!!!!!! Why didn't anyone say so before (that I know of)?!?!?!  This is
the first explanation i've seen that, to me, "really says it."  Of course,
unfortunately, I'm sure someone will come along and correct your explanation
(IT ALWAYS HAPPENS), resulting in a confusing contradiction that will leave
me in a vacuum again.

Anyway, what you said above is what I was *hoping* was the answer.  Thats
why I had earlier said this:

> Perhaps this is just a semantics issue.  Perhaps a variable "containing" a
> table, internally is implemented somewhat like a pointer, but is exposed
> lua as a regular variable.  If that is the case (and I suppose that is
> correct), then you could say tables are passed by value, yet modifying the
> content of the passed table will indeed affect the original (as if it were
> pass by reference).

But nobody said "yes, you got it."  :(

Anyway, you said:

The acid test of whether a language is pass-by-reference or
pass-by-value, BTW, is whether it can swap with a function such as

function swap(a, b) local tmp = a; a = b; b = tmp; end

In Lua, you will find that the values a and b in the function which
calls swap() will still point to the same tables afterwards. (Very few
modern languages are pass-by-reference.)

This, like your prior comment, is actually helpful and intelligable to me.
Unfortunately, as I said before, I fear another expert is going to come
along and contradict what you are saying.  Its almost like there's an
esoteria police that interjects acadamia unto confusion, whenever the hoi
polloi are beginning to understand argument passing. <grin>