lua-users home
lua-l archive

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


Yep, I can definitely see how it'd be impractical to implement.

I guess the alternative would be to come at it from the opposite direction by letting mutators to replace the thing they're mutating. Eg:

metatable.__newindex = function( thing, index, value )
	local result = thing:copy()
	rawset( thing, index, value )
	return result
end

v.x = 100		-- => "v = getmetatable(v).__newindex( v, 'x', 5 ) or v"


Dunno how implementable this is though.

Mike F



On 22 Jul 2008, at 19:15, Mark Hamburg wrote:

It definitely takes slightly different thinking to deal with mutability v immutability. One way to do this is to have immutable types plus a calculator object which supports an API for working on mutable values. When you ask the calculator for a value, it would make an immutable copy. There are probably also useful patterns from the Java community that would apply here since it has the same issues with all complex types being pass-by-reference.

The performance problem with your approach is that it would require attempting to invoke the metamethods on every parameter ever passed and every value ever assigned.

Mark