lua-users home
lua-l archive

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


Hi all,

This code runs and yields two point objects -- one at 1,1 and one at 80,25:

================================
#!/usr/bin/lua 

do
	POINT = {}
	POINT.new = function(pnt, x, y)
		local pt = {}
		pt.x = x
		pt.y = y
		function pt.display(pnt, comment)
			print(comment)
			print(pnt.x)
			print(pnt.y)
			print("")
		end
		return pt
	end
end

pul = POINT:new(1,1)
plr = POINT:new(80,25)

print(type(pul))

pul:display("Upper left.")
plr:display("Lower right.") -- This fails if you uncomment it

pul.x=2000
pul:display("Upper left after strongarm.")

================================

Pretty cool, but I couldn't figure out a way to make x and y private (at which 
time getx() and setx() etc would need to be added. Do I need to put all of 
POINT.new in a closure of some kind? Do I need to use metatables? From what 
I've heard it's necessary to use metatables to do inheritance.

Thanks

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt