lua-users home
lua-l archive

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


I strongly endorse this general concept and had been planning on writing something similar up. The set example is a really great example. The key point I'd thought about had to do with allowing one to construct object systems in which one could readily detect failure to use a colon when sending messages. It also allows for mixing efficient method inheritance with property accessors because __mcall could lead to a table of methods while __index could lead to a function that did the property accessor logic. Finally, it makes it easier to write methods that can know to what object they are being applied because they can't be accessed as "loose" functions. That's a help when writing efficient bridges to native code.

The semantics of __mcall would be something like the following (which essentially corresponds to the self opcode):

	function mcallprep( t, k )

		local mt = getmetatable( t )
		local mcall = mt and mt.__mcall

		if mcall then
			if type( mcall ) == 'table' then
				return mcall[ k ], t
			else
				local f, o = mcall( t, k )
				return f, o
			end
		end

		return t[ k ], t -- Uses standard lookup

	end

There should be discussion about whether this should first look at rawget( t, k ) before turning to the metamethods. I chose not to here, but that was essentially an arbitrary choice. The calling logic for the case where mcall isn't a table also potentially needs discussion but is designed to match with the expected behavior of the opcode it virtualizes. This also probably needs thought as to how one builds inheritance hierarchies.

Finally, one could argue that adding this feature essentially necessitates method currying and the obj:[ method ] extensions because otherwise one would lose the ability to call arbitrary methods based on a parameter or to cache the results of a method lookup.

Mark

P.S. I sometimes class all of this stuff together under the label "embracing the colon operator" -- i.e., stop wishing that period did a message send and embrace the colon operator.