# HG changeset patch # User IBBoard # Date 1505329912 -3600 # Node ID e8407d4e72dd25405fba9853f9ee960b3b7fca55 # Parent 86668d32e1629b0f4404f4c50b14aa9fb42c8a21 Add code on reflection - basically involves the object returned by "proto" message diff -r 86668d32e162 -r e8407d4e72dd 2-Io/day1-self-study.io --- a/2-Io/day1-self-study.io Tue Sep 12 20:58:51 2017 +0100 +++ b/2-Io/day1-self-study.io Wed Sep 13 20:11:52 2017 +0100 @@ -42,6 +42,7 @@ myvar proto slotNames println myvar proto proto slotNames println # If I knew enough Io then I'd do a recursion loop and walk up until proto == Object +# Update: See day2-reflection.io! diff -r 86668d32e162 -r e8407d4e72dd 2-Io/day2-reflection.io --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/2-Io/day2-reflection.io Wed Sep 13 20:11:52 2017 +0100 @@ -0,0 +1,27 @@ +# Reflection in Io is about working out what goes on in slots +# We're now going to create an "ancestors" method that we needed +# on day 1 to find all slots! +Object ancestors := method( + # Store our prototype + prototype := self proto + # If we're not at the top + if (prototype != Object, + # Then print the type name as a header + writeln("Slots of ", prototype type,"\n--------------------") + # And iterate the slots with foreach() before printing with writeln() + # TODO: Find the difference between println and writeln, other than "message vs function" + prototype slotNames foreach(slotName, writeln(slotName)) + # Blank line + writeln + # Recurse! + prototype ancestors + ) +) + +Animal := Object clone +Animal speak := method("*ambiguous animal noise" println) +Duck := Animal clone +Duck speak := method("Quack-quack!" println) +Duck walk := method("waddle" println) +donald := Duck clone +donald ancestors \ No newline at end of file