comparison 2-Io/day1-methods.io @ 32:4d3769ac447c

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