[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: metamethods for assignment and type conversion
- From: Adrien de Croy <adrien@...>
- Date: Tue, 14 Aug 2007 08:38:32 +1200
*Lua list <email@example.com
*neither of these affect the C++ object that is wrapped by var1, var1
instead gets a new pointer to wrap (being a copy), and the old one is
If you want to modify the pointer that var1 wraps, then var1 needs to be
involved in the function that does the assignment.
True, I guess I was thinking more of a copy constructor than an
I guess a var1:CopyFrom() (or :Assign()) really is the best you can do
then, sorry for the misinterpretation.
Having said that, personally, I like the clarity of this over having
to think every time I do A = B whether or not this is:
- Make A refer to the same object that B refers to
- Create a new object from B and assign a reference to it to A,
overwriting the reference stored in A before.
- A continues to refer to the same object as before but has had some
of its internal fields tweaked such that they've been copied from B.
Worse yet, if a metamethod were defined to do something like this, the
rules could vary depending on the types of A and B.
as to whether that's worse, that's another debate.
C++ has operator= , people familiar with C++ are used to the knowledge
that something as simple as
A = B
needs to be interpreted according to the types of A and B: if they are
pointers, then A points to the object B did. If they are objects with
an operator= defined, then that operator is called. If they are
structures, then the compiler already generated a copy function for them
that just copies all the members naively.
The thing with Lua, is that it is targeted as an extension language for
That means that the target market for the lua language is people who
wish to customise apps. This is customers - the general populace. The
relatively non-techies, or at least non-programmers
So is it more intuitive, if you wish to copy an object, to write A=B, or
is it more intuitive to write A.Assign(B), or B.CopyTo(A)?
IMO, someone that writes A = B wants A to be B, not A to be another
reference to B (which is pretty much pointless, since you already have B).
writing a metamethod for this would then be an extension to the language
to allow the developer to get the language to behave like they wish. By
default the metamethod wouldn't be there, it would only be there by
explicit design in specific cases, where it made obvious sense to do so.
Adrien de Croy - WinGate Proxy Server - http://www.wingate.com