Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 2-Io/day1-self-study.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 | 15eb99e79dd4 |
children |
rev | line source |
---|---|
35 | 1 #Truthiness: |
2 # We already know that 0 is truthy | |
3 (true and 0) println | |
4 # Empty strings are apparently truthy | |
5 (true and "") println | |
6 # As are empty lists | |
7 (true and list()) println | |
8 # But nil is falsey | |
9 (true and nil) println | |
10 # So basically anything except `false` and `nil` is truthy | |
11 | |
12 # = vs := vs ::= | |
13 # Obvious part so far is that you must create with := before being able to assign with plain = | |
14 myvar := Object clone | |
15 myvar println | |
16 myvar = Object clone | |
17 # Docs say: | |
18 # ::= Creates slot, creates setter, assigns value | |
19 # := Creates slot, assigns value | |
20 # = Assigns value to slot if it exists, otherwise raises exception | |
21 # But what is a "setter" in this context? | |
22 myvar foo ::= Object clone | |
23 myvar println | |
24 # It seems we get a "setX" method, which helps us do some things in certain contexts: | |
25 # https://stackoverflow.com/a/5977757 | |
26 | |
27 # What slots does a prototype support? Direct ones are human readable in the output | |
28 myvar proto println | |
29 # But they're not listed indirectly | |
30 SubClass := Object clone | |
31 myvar2 := SubClass clone | |
32 myvar2 proto println | |
33 # Can we find it out programattically? | |
34 # It's not recursive, but the type should tell us: | |
35 myvar2 type proto println | |
36 # Except the value of `type` is just a string (or Sequence in Io), so it doesn't | |
37 myvar2 type proto type println | |
38 # Or, of course, a Prototype is a named object that starts with a capital letter | |
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 |
38
e8407d4e72dd
Add code on reflection - basically involves the object returned by "proto" message
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
45 # Update: See day2-reflection.io! |
36
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 |
15eb99e79dd4
Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents:
35
diff
changeset
|
49 #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
|
50 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
|
51 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
|
52 myvar2 perform("method1") |
15eb99e79dd4
Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents:
35
diff
changeset
|
53 myvar2 perform("method2") |
15eb99e79dd4
Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents:
35
diff
changeset
|
54 |
15eb99e79dd4
Call methods by name and fix slot name printing
IBBoard <dev@ibboard.co.uk>
parents:
35
diff
changeset
|
55 |
35 | 56 |
57 # Is Io strongly or weakly typed? | |
58 (1 + 1) println | |
59 (1 + "one") println | |
60 # Exception: argument 0 to method '+' must be a Number, not a 'Sequence' | |
61 # Therefore strongly typed |