[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Deep __newindex on assignment?
- From: Alexander Gladysh <agladysh@...>
- Date: Wed, 23 Dec 2009 00:52:58 +0300
> I had considered using a proxy, but won't that make it impossible to ever get a simple copy of the value? If I understand the sample code you linked that proxy would result in the following code modifying the original property bag:
Yes, it would.
> local pos = propBag.Position -- pos is now the proxy object wrapping the Point userdata
> pos.z = 10 -- this will use the proxy's __newindex thus setting the value in propBag
> otherBag.Position = pos
> assert(propBag.Position ~= otherBag.Position) -- this would fail as both were modified
This assertion is not Lua-way and it would be counter-intuitive for Lua users.
I firmly believe that whenever possible (if not in all cases) userdata
*must* act as if it was a table. Tables are not copied on assignment.
> With the above syntax I wouldn't want the original propBag to be modified. It seems like I would need some way of knowing if __index is being called due to a term on the left side of an assignment operator. Then I could use the proxy wrapper only in cases of eventual assignment, otherwise I could return an unwrappered value which effectively acts as a copy. Is that possible?
I do not think that it is a good idea.
Also, to the best of my knowledge, it is not possible within pure Lua.
You'd have to use Metalua for that.