Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- A table can have a metatable that gives the table
- -- operator-overloadish behavior. Later we'll see
- -- how metatables support js-prototypey behavior.
- f1 = {a = 1, b = 2} -- Represents the fraction a/b.
- f2 = {a = 2, b = 3}
- -- This would fail:
- -- s = f1 + f2
- metafraction = {}
- function metafraction.__add(f1, f2)
- sum = {}
- sum.b = f1.b * f2.b
- sum.a = f1.a * f2.b + f2.a * f1.b
- return sum
- end
- setmetatable(f1, metafraction)
- setmetatable(f2, metafraction)
Advertisement
Add Comment
Please, Sign In to add comment