annotate 2-Io/day3-method_missing.io @ 103:98be775c533c default tip

An odd "non-determinism" example from StackOverflow It is clever, but doesn't make much sense as to how it gets its results
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Jul 2019 13:44:13 +0100
parents 46a807289724
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
48
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 # Altering "forward" in Io is apparently similar to overriding
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 # method_missing in Ruby. Except with more to go wrong because
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 # we have prototypes and not classes.
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 # Building arbitrary (basic) XML using a Builder prototype that
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 # assumes all missing methods are XML tags
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 Builder := Object clone
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 # This is the dangerous method
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 Builder forward := method(
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 writeln("<", call message name, ">")
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 call message arguments foreach(
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 arg,
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 # This will trigger another call to forward
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 # It's not clear why "writeln" would return a string rather than write to the console
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 # But apparently it does. Because poorly documented Io.
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 content := self doMessage(arg)
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 # No strong type checking here - it's all based on string names
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 if (content type == "Sequence", writeln(content))
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 )
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 writeln("</", call message name, ">")
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 )
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 Builder ul(
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 li("Obscure"),
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 li("Undocumented"),
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 li("Unclear")
46a807289724 Add a noddy "builder" to show overriding "forward" for missing methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 )