lua-users home
lua-l archive

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


In a sign of how class-based programming contaminated me with respect to prototype-based programming, I've long focused way too heavily on mechanisms like parent* links in Self or whatever the JavaScript equivalent is as a way to do inheritance. Metatables in general in Lua fell into this category of thinking. Some time relatively recently, it finally got through to me that the prototype model is really about duplicating whole objects and that there generally shouldn't be any ongoing relationship between an object and the prototype object from which it was cloned.

So, what does this mean in general? It means we share metatables because they change rarely and its efficient to do so. We use __index entries either because of a need to implement particular behavior or again because there is a body of essentially unchanging material that we want to make efficient to copy. But thinking in terms of inheritance hierarchies and mechanisms misses the point.

Now, given shared material, what is probably needed is a way to force a deeper clone of the metatable or its __index entry (or other pieces) at clone time. For example:

	clone( obj, overrides )

Where we might write:

	clone( obj, {
		objfield = "foo",
		__metatable = {
			__tostring = function( o ) ... end
		}
	} )

Mark