annotate 2-Io/day1-self-study.io @ 36:15eb99e79dd4

Call methods by name and fix slot name printing
author IBBoard <dev@ibboard.co.uk>
date Tue, 12 Sep 2017 20:07:04 +0100
parents 22edfbf3b8bd
children e8407d4e72dd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 #Truthiness:
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 # We already know that 0 is truthy
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 (true and 0) println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 # Empty strings are apparently truthy
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 (true and "") println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 # As are empty lists
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 (true and list()) println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 # But nil is falsey
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 (true and nil) println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 # So basically anything except `false` and `nil` is truthy
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 # = vs := vs ::=
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 # Obvious part so far is that you must create with := before being able to assign with plain =
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 myvar := Object clone
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 myvar println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 myvar = Object clone
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 # Docs say:
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 # ::= Creates slot, creates setter, assigns value
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 # := Creates slot, assigns value
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 # = Assigns value to slot if it exists, otherwise raises exception
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 # But what is a "setter" in this context?
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 myvar foo ::= Object clone
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 myvar println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 # It seems we get a "setX" method, which helps us do some things in certain contexts:
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 # https://stackoverflow.com/a/5977757
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 # What slots does a prototype support? Direct ones are human readable in the output
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 myvar proto println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 # But they're not listed indirectly
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 SubClass := Object clone
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 myvar2 := SubClass clone
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 myvar2 proto println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 # Can we find it out programattically?
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 # It's not recursive, but the type should tell us:
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 myvar2 type proto println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 # Except the value of `type` is just a string (or Sequence in Io), so it doesn't
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 myvar2 type proto type println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 # Or, of course, a Prototype is a named object that starts with a capital letter
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 # So we can just use slotNames to get them programmatically (but that only shows direct slots)
36
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
40 myvar slotNames println
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
41 # And to get the parent ones then we just chain "proto"
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
42 myvar proto slotNames println
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
43 myvar proto proto slotNames println
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
44 # If I knew enough Io then I'd do a recursion loop and walk up until proto == Object
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
45
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
46
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
47
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
48 #Execute methods by slot name - use "perform"
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
49 SubClass method1 := method("Method 1 was called" println)
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
50 SubClass method2 := method("Method 2 was called" println)
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
51 myvar2 perform("method1")
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
52 myvar2 perform("method2")
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
53
15eb99e79dd4 Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
54
35
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 # Is Io strongly or weakly typed?
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 (1 + 1) println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 (1 + "one") println
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 # Exception: argument 0 to method '+' must be a Number, not a 'Sequence'
22edfbf3b8bd Add most of the self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 # Therefore strongly typed