Created
January 23, 2011 22:37
-
-
Save tafsiri/792523 to your computer and use it in GitHub Desktop.
mixins in Io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Minimal mixins in Io! | |
Object mix := method(obj, | |
#grab all the objects methods and add them to the target | |
#the target is the object mixing in the mixin | |
mixer := call target | |
obj slotNames foreach(slotName, | |
if(obj getSlot(slotName) type == "Block" | |
, mixer setSlot(slotName, obj getSlot(slotName)) | |
#ignore properties for the moment | |
) | |
) | |
) | |
#alternative way to do mixins | |
Object include := method(obj, | |
mixer := call target | |
mixer appendProto(obj) | |
) | |
Comparator := Object clone do( | |
< := method(other, compareTo(other) < 0) | |
> := method(other, compareTo(other) > 0) | |
== := method(other, compareTo(other) == 0) | |
) | |
StrangeNum := Object clone do( | |
init := method( | |
mix(Comparator) #Mix in the comparator | |
) | |
#for some bizzare reason we want to sort by the squares of [value] | |
compareTo := method(other, | |
value squared compare(other value squared) | |
) | |
) | |
low := StrangeNum clone | |
low value := 2 | |
high := StrangeNum clone | |
high value := -3 | |
(high value < low value) println # => true | |
(high < low) println # => false, uses our compareTo method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment