[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Arguments by reference?
- From: Glenn Maynard <glenn@...>
- Date: Thu, 1 Sep 2005 14:09:12 -0400
On Thu, Sep 01, 2005 at 10:48:11AM -0700, Brent Arias wrote:
> Wow! Now this is bewildering:
>
> ------------------
> They might look like they're being passed by reference, but they're
> not. All arguments in Lua are passed by value. They are not passed by
> copy, so when you pass a table (by value) you are passed the same
> table,
> ------------------
>
> Not passed by reference? And not passed by copy either? I don't get it.
> Those are the only two options. If its not a reference/pointer, and its not
> a copy of the value...then...what?
>
> Perhaps this is just a semantics issue. Perhaps a variable "containing" a
> table, internally is implemented somewhat like a pointer, but is exposed to
> 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 a
> pass by reference).
At least by my notion of semantics, all variables are passed by copy, as in
C. The pointer comparison is accurate:
void foo( int *p ) { p[0] = 1; p = NULL; } // C
function foo( p ) p[0] = 1; p = nil; end -- Lua
are similar in behavior. These pass by copy, but it's a shallow copy, not
a deep copy.
--
Glenn Maynard