Passing an object as a parameter to a function and setting it to nil

I’ve been trying to wrap my head around what happens when you set an object passed as a parameter to nil.

So I did a test…

local function removeObject(obj) obj.test = 2 obj = nil end local photoPicker = display.newGroup() -- any display object photoPicker.test = 1 removeObject(photoPicker) if (photoPicker == nil) then print("PhotoPicker is nil") else print("PhotoPicker is not nil") print("Test: ", photoPicker.test) end

I’m surprised that after calling removeObject, photoPicker is not nil, but the test field was changed.

Why is that?  I assume whatever that reason, that’s probably why display.remove doesn’t automatically nil the object.  (Because I was wondering, why doesn’t display.remove automatically do that?)

Dave

In Lua, there is a distinction between variables (and table keys, for that matter) and values. A variable is basically just a location with a name, which holds some value ( nil being one such possibility). In this case, variables photoPicker and obj initially share the same value (the reference to the display group), but then a new value, nil , is placed in the location belonging variable obj.

The function call machinery just pre-populates the parameter variables with the same values as its arguments (and implicitly nil’s them out afterward).

This is call-by-value semantics, see e.g. http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value

(Apologies for “reference” getting overloaded in this post. These are the accepted terminology in both cases, but refer to separate things.)

I understood the concept of passing by reference and by value, but I did not understand what happens when that reference is destroyed, so thanks for the reply!

Also, that stackoverflow answer is just a stellar explanation of the concept.

Thanks again,

Dave

In Lua, there is a distinction between variables (and table keys, for that matter) and values. A variable is basically just a location with a name, which holds some value ( nil being one such possibility). In this case, variables photoPicker and obj initially share the same value (the reference to the display group), but then a new value, nil , is placed in the location belonging variable obj.

The function call machinery just pre-populates the parameter variables with the same values as its arguments (and implicitly nil’s them out afterward).

This is call-by-value semantics, see e.g. http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value

(Apologies for “reference” getting overloaded in this post. These are the accepted terminology in both cases, but refer to separate things.)

I understood the concept of passing by reference and by value, but I did not understand what happens when that reference is destroyed, so thanks for the reply!

Also, that stackoverflow answer is just a stellar explanation of the concept.

Thanks again,

Dave