changeset 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 86668d32e162
children c48e6e454991
files 2-Io/day1-self-study.io 2-Io/day2-reflection.io
diffstat 2 files changed, 28 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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!
 
 
 
--- /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