Please explain this assignment stmt

I just had 2 thoughts.  I know it happens from time to time. 

1.  That loop can be used to combine tables but also to implement simple OOP inheritance. 

2.  Since you delete variables in Lua by setting them to nil, it seems that loop is actually deleting a bunch of blank variables.  

If that was the intent then great .  Can you even initialize a Lua variable to nil?  That doesn’t seem like a thing you can actually do.  

You  can set a variable to nil. 

It simply clears the variable.

If that variable was referring to a previously deleted display object, or if it was referring to a table and the object/table has no other references, it can now be garbage collected (different from deletion).

local obj = display.newCircle( 10, 10, 10 ) display.remove( obj ) -- deleted, but remaining stub table can't yet be garbage collected obj = nil -- now garbage collection can free memory associated with stub of obj

local junk = { } junk[1] = { a, b, c } junk[2] = " junk[1] = nil -- Table that was at 1 can now be garbage collected. junk[2] = nil -- String at 2 can now be garbage collected. -- Note: Table junk has no entries, but is still valid. junk = nil -- Now the table can be garbage collected -- Tip,if we skipped prior nil assignments, the table at entry 1 would be -- garbage collected at this point.

@laurasweet8888

  1. yep, what you have here is a “shallow prototype” type of OOP.  (ie, “shallow” versus “deep”, fe if one of the things inherited is itself a table, then the “image” would just get a reference to the same table, not an entire fresh copy of the table - that would be a “deep” copy).

  fwiw, you could expand upon it for OOP use by taking variable arguments (instead of just the single “properties”) and inheriting from multiple sources in order, fe if you wish to set up a good environment for implementing composition-based OOP (fe dog=inherit(animal,canine,tamed,candotricks).

  or you could just use 30log

  1. yes, except that it won’t actually work as written.  reread my prior comments, then work it through manually - its ternary formulation is flawed.  it might as well just read “image[k] = v” (because the “and nil” portion of the expression will NEVER evaluate true, so it’ll ALWAYS return “or v”)