object-oriented programming

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

object-oriented programming

fschmidt
Administrator
I just read the "object-oriented programming" chapter in the latest Lua book.  I think Lua handles this poorly.  Lua's use of ":" for objected-oriented access is confusing and I left it out of Luan.  I don't think most Luan users should do object-oriented programming, but if they do, they should do it like this:
function newAccount(initialBalance)
	local self = {}
	local balance = initialBalance or 0

	function self.withdraw(v)
		balance = balance - v
	end

	function self.deposit(v)
		balance = balance + v
	end

	function self.getBalance()
		return balance
	end

	return self
end

acct = newAccount(100.00)
acct.withdraw(40.00)
print(acct.getBalance())
This is how I would implement the example given in the Lua book.
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture