lua-users home
lua-l archive

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


> I was thinking that just by extending the Lua syntax
> table::function() -> methods(table):function()
> That way the new colon syntax is related to the old 
> mnemonically.  From the
> C++ perspective your way makes sense, but I don't think 
> asking people to
> remember syntax with reference to C++ is a good thing.

Ah, I do like the notion behind that.  I only chose what I did because
the C++ style was on my mind, that's all.

> Too bad .. is concatenation or you could use t..x and t::x to 
> mean methods(t).x and methods(t):x respectively.

Yeah, this wouldn't be as nice.  Obviously, if method tables were
adopted into Lua, some discussion would be in order to determine the
correct syntax for the calls.

---------------------
And, for what it is worth, here is an implementation of a "method table"
using tags in the current LuaState distribution:

methodTable =
{
	func = function(self)
		print("Hi", self.a)
	end,

	func2 = function()
		print("Hi")
	end
}

methodTable2 = CopyTable(methodTable)

function methodTable2:func3()
	print("Hello", self.b)
end

myTable = { a = 5 }
methods(myTable, methodTable)
myTable::func2()
myTable->func()

myTable2 = { a = 10, b = 20 }

function myTable2:func()
	print("In myTable2:func()", self.b)
end

methods(myTable2, methodTable2)
myTable2::func2()
myTable2:func()				-- Note the difference between
myTable2->func()				-- these two lines
myTable2->func3()

----------------------------------------------------------
Here is the same "method table" using tags in regular Lua:

methodTable =
{
	func = function(self)
		print("Hi", self.a)
	end,

	func2 = function()
		print("Hi")
	end
}

methodTable2 = CopyTable(methodTable)

function methodTable2:func3()
	print("Hello", self.b)
end

----
function myTable_index(table, index)
	return methodTable[index]
end

function myTable2_index(table, index)
	return methodTable2[index]
end

tableTag = newtag()
settagmethod(tableTag, "index", myTable_index)

myTable = { a = 5 }
settag(myTable, tableTag)

myTable.func2()
myTable:func()

table2Tag = newtag()
settagmethod(table2Tag, "index", myTable2_index)

myTable2 = { a = 10, b = 20 }

function myTable2:func()
	print("In myTable2:func()", self.b)
end

settag(myTable2, table2Tag)

myTable2.func2()
myTable2:func()
myTable2:func3()

--------------------------------------------------------------

The biggest difference between the two is speed.  In order for the index
tag method to be called, the VM has to first determine that func, func2,
or func3 isn't in the table.  Then it sets up all state to call the tag
method, makes the function call, which causes another virtual machine
execution, and finally retrieves the value.

In the method table syntax, no tag methods are called and you already
explicitly told the VM to look in the method table.  In fact, LuaState's
implemention of the VM instructions for the method table actually end up
being a touch faster than a regular table lookup.

One more item of note is the fact that myTable2 contains a function
called func().  So does the method table.  In the tag method setup,
there is no way to call the method table's func().  They are easily
distinguished between with the method table.

Josh