Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 2-Io/day1-methods.io @ 77:7bab4843aec6
Add roulette with auto-restarting process
(And black-magic auto-registering of atoms)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 03 Feb 2018 20:42:51 +0000 |
parents | 4d3769ac447c |
children |
rev | line source |
---|---|
32
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 # Methods are objects, defined by some new notation: |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 # method(object message[…]) |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 # This would show something in REPL but is useless in normal execution |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 method("This does nothing but print something, but with the line-break" println) |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 # Methods are of type "Block" |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 method() type println |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 # And they can be assigned to slots like values |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 Car := Object clone |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 ferrari := Car clone |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 Car drive := method("Vroom" println) |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 # Note that "cloning" isn't a one-time thing - we modified Car after creating |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 # ferrari, but it now has the `drive` method anyway. This is good, but |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 # doesn't *quite* match the naming |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 ferrari drive |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 # You can also retrieve slots (methods or values), which walks up the heirarchy if necessary |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 ferrari getSlot("drive") println |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 # And you can find out what its prototype is |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 # (i.e. what it inherited from and what exists) |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 ferrari proto println |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 # But what if we add something to the ferrari? that isn't covered yet |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 ferrari name := "My Fast Car" |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 ferrari proto println |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 ferrari name println |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 # It seems to only cover the parent stuff |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 # There's also a "master namespace" that all the objects get created in: |
4d3769ac447c
Add notes and code on methods in Io
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 Lobby println |