Summary
In this lesson, you learned that:
- A table can hold functions next to its data. Such a table is an object.
function farm.describe(f) … endis short forfarm.describe = function(f) … end.stringandmathare tables of functions, too. - The colon passes the object itself:
crop:grow()is short forcrop.grow(crop), andfunction crop:grow()forfunction crop.grow(self). Inside such a method,selfis the object it was called on. A dot instead of the colon in the call givesattempt to index a nil value (local 'self'). - A constructor is a function that creates an object and returns it. Without
return, you getnil:attempt to index a nil value (local 'carrot'). setmetatable(t, mt)gives a table a metatable. Its__indexsays where to look for keys thattdoesn't have. That's how objects share methods, and how a text findsstring.upperfors:upper().- A class is a table that holds the methods and is the metatable of its instances:
local Crop = {},Crop.__index = Crop, a constructorCrop.newthat callssetmetatable(crop, Crop), and methods such asfunction Crop:grow(). WithoutCrop.__index = Crop, you getattempt to call a nil value (method 'grow'). - Metamethods make objects work with the rest of Lua:
__tostringwithprintandtostring(but not with..),__eqwith==,__ltwith<,>andtable.sort,__lewith<=and>=, and__addwith+. - A class inherits from another with
setmetatable(Tree, {__index = Crop}). Its constructor calls the parent's constructor and thensetmetatable(tree, Tree). A method with the same name overrides the parent's, andCrop.describe(self)calls the parent's version. Metamethods aren't inherited.
In the next lesson, Coroutines, you'll write functions that can pause in the middle and continue later, for example for a character who does one thing each day.
But first, put this lesson together. Emma is selling at the market, and the program in the editor uses a class Product that doesn't exist yet, so it ends with attempt to index a nil value (global 'Product').
Instructions
Above stall, write the class Product. Its constructor Product.new(name, price, count) creates a product with these three keys, and its __tostring describes it as below. The program prints:
Pumpkin: 3 left, 20 coins each
Carrot: 10 left, 5 coins each
Strawberry: 6 left, 12 coins eachAdd a method Product:sell(amount). It sells amount pieces, but never more than are left, and returns the coins Emma gets for them. Below the loop, sell 2 pumpkins and 15 carrots (only 10 are left), add up the coins, and print them:
Coins: 90In the evening, Emma wants to see what's left, cheapest first. Add __lt to Product so that it compares prices, sort the stall with table.sort, and print it again. The program ends like this:
Coins: 90
Carrot: 0 left, 5 coins each
Strawberry: 6 left, 12 coins each
Pumpkin: 1 left, 20 coins eachStart programming for free
9/9