lua-users home
lua-l archive

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


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

Just for completion, the Sol version ;-)  (and some comments)

-----Sol version-----
require "classes"  -- only thing to know: Table=methods({})

methodTable = Table:class {  -- inherit methods from Table (append, ...)
	function func(self)
		print("Hi", self.a)
	end

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

methodTable2 = methodTable:class {  -- inherit methods from methodTable
	function func3(self)
		printf("Hello", self.b)
	end
}

myTable = methodTable { a=5 }  -- create instance
myTable:func()
myTable:func2()		-- passes myTable, but how cares? ;->
-- rational: you have to write (myTable:func2)() but Sol does not
-- allow () as the start of a statement.  But this is ok:
--   x = (myTable:func2)()
-- anyway, a method that doesn't take a self (static class function?)
-- shouldn't be called via an instance.  this should be used:
--   methodTable.func2()
-- or, going back to a "PS:" of one of my previous posts:
--   myTable:this_class.func2()


myTable2 = methodTable2 { a=10, b=20 }

function myTable2.func(self)	-- an instance's private function
	print("In myTable2.func()", self.b)
end

methodTable2.func2()	-- actually redirected to methodTable by inheritance
-- x = (myTable2:func2)()  would work too, but see comment above.
myTable2.func(myTable2)	-- yes, this one has to be handcoded...
myTable2:func()
myTable2:func3()
-----------------------------

IMHO, having 4 different field access operators is confusing!
I could understand that one want's another operator to preserve backward
compatibility.  So make it '->' (will give the minimum confusion) and mark
the ':' as "for compatibility use only" (remember, it was ment as a method
operator to give OO look'n'feel in the first place).  I think that you won't
need it very often anyway.  And about your fourth one see the comments in
the code above.

Ciao, ET.