annotate 2-Io/day2-reflection.io @ 38:e8407d4e72dd

Add code on reflection - basically involves the object returned by "proto" message
author IBBoard <dev@ibboard.co.uk>
date Wed, 13 Sep 2017 20:11:52 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 # Reflection in Io is about working out what goes on in slots
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 # We're now going to create an "ancestors" method that we needed
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 # on day 1 to find all slots!
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 Object ancestors := method(
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 # Store our prototype
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 prototype := self proto
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 # If we're not at the top
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 if (prototype != Object,
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 # Then print the type name as a header
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 writeln("Slots of ", prototype type,"\n--------------------")
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 # And iterate the slots with foreach() before printing with writeln()
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 # TODO: Find the difference between println and writeln, other than "message vs function"
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 prototype slotNames foreach(slotName, writeln(slotName))
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 # Blank line
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 writeln
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 # Recurse!
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 prototype ancestors
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 )
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 )
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 Animal := Object clone
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 Animal speak := method("*ambiguous animal noise" println)
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 Duck := Animal clone
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 Duck speak := method("Quack-quack!" println)
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 Duck walk := method("waddle" println)
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 donald := Duck clone
e8407d4e72dd Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 donald ancestors