ForbodingAngel

Untitled

Mar 12th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. -- A table can have a metatable that gives the table
  2. -- operator-overloadish behavior. Later we'll see
  3. -- how metatables support js-prototypey behavior.
  4.  
  5. f1 = {a = 1, b = 2} -- Represents the fraction a/b.
  6. f2 = {a = 2, b = 3}
  7.  
  8. -- This would fail:
  9. -- s = f1 + f2
  10.  
  11. metafraction = {}
  12. function metafraction.__add(f1, f2)
  13. sum = {}
  14. sum.b = f1.b * f2.b
  15. sum.a = f1.a * f2.b + f2.a * f1.b
  16. return sum
  17. end
  18.  
  19. setmetatable(f1, metafraction)
  20. setmetatable(f2, metafraction)
Advertisement
Add Comment
Please, Sign In to add comment