Guest User

Untitled

a guest
Jul 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #Minimal mixins in Io!
  2. Object mix := method(obj,
  3. #grab all the objects methods and add them to the target
  4. #the target is the object mixing in the mixin
  5. mixer := call target
  6. obj slotNames foreach(slotName,
  7. if(obj getSlot(slotName) type == "Block"
  8. , mixer setSlot(slotName, obj getSlot(slotName))
  9. #ignore properties for the moment
  10. )
  11. )
  12. )
  13.  
  14. #alternative way to do mixins
  15. Object include := method(obj,
  16. mixer := call target
  17. mixer appendProto(obj)
  18. )
  19.  
  20. Comparator := Object clone do(
  21. < := method(other, compareTo(other) < 0)
  22. > := method(other, compareTo(other) > 0)
  23. == := method(other, compareTo(other) == 0)
  24. )
  25.  
  26. StrangeNum := Object clone do(
  27. init := method(
  28. mix(Comparator) #Mix in the comparator
  29. )
  30.  
  31. #for some bizzare reason we want to sort by the squares of [value]
  32. compareTo := method(other,
  33. value squared compare(other value squared)
  34. )
  35. )
  36.  
  37. low := StrangeNum clone
  38. low value := 2
  39. high := StrangeNum clone
  40. high value := -3
  41.  
  42. (high value < low value) println # => true
  43. (high < low) println # => false, uses our compareTo method
Add Comment
Please, Sign In to add comment