lua-users home
lua-l archive

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


Adrien wrote:

for userdata and lightuserdata, it makes perfect sense to
use the same syntax to copy one userdata to another one
the same as you would a string value, rather than a
function to do it.

In an assignment like

 dest = src

the value in src is placed in dest, whether that value is a
string or a table or whatever.  The thing that makes this
hard to see is that tables are mutable whereas strings are
interned and immutable.

Compare this code:

 src = {} -- Call this table 0000DEAD.
 dest = {} -- Call this table 0000BEAF.
 dest = src -- On this line the value 0000DEAD is placed
   -- into dest (while remaining in src) and nothing is
   -- done to the value 0000BEAF.

with this code:

 src = "dead"
 dest = "beaf"
 dest = src -- On this line the value "dead" is placed into
   -- dest (while remaining in src) and nothing is done to
   -- the value "beaf".

For more on a related topic see an old post from me:

 http://lua-users.org/lists/lua-l/2005-09/msg00052.html

And Rici's excellent essay-length response to it:

 http://lua-users.org/lists/lua-l/2005-09/msg00056.html

To make sure I'm not misassuming something: As I understood
it, your example function

function dosomething(object1, object2)
   object2.somemember = "hello";
   object1 = object2; -- deep copy in host via __assign
                      -- metamethod rather than table reference
end

was meant to mutate the value in object1 to make it look
like object2, rather than "overwriting" that value with a
freshly created value.  (I assumed this because object1 is
never returned.)  If instead you meant object1's value to be
overwritten, then that wouldn't violate the "assignment
mutates variables not their values" rule, so that part of my
warning wouldn't apply.

The part about assignment being blocked still would, though.
As you said, __assign could be used judiciously, but why not
just do:

 object1 = deepcopy(object2)

and leave the assignment syntax to do what it normally does?

but it doesn't really make that much sense that lua
exposes all the other operators except assignment.

Unlike in some other languages, in Lua assignment is not an
operator, it's a statement.  (And, for that matter, there
are no metamethods for the "and", "or", & "not" operators.)

I've already coded part of the __cast metamethod.  It's
working very well, I'm able to automatically convert
tables into strings with a callback now.

Good.

--
Aaron
http://arundelo.com/

_________________________________________________________________
More photos, more messages, more storage?get 2GB with Windows Live Hotmail. http://imagine-windowslive.com/hotmail/?locale=en-us&ocid=TXT_TAGHM_migration_HM_mini_2G_0507