view 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
line wrap: on
line source

# Methods are objects, defined by some new notation:
# method(object message[…])

# This would show something in REPL but is useless in normal execution
method("This does nothing but print something, but with the line-break" println)
# Methods are of type "Block"
method() type println
# And they can be assigned to slots like values
Car := Object clone
ferrari := Car clone
Car drive := method("Vroom" println)
# Note that "cloning" isn't a one-time thing - we modified Car after creating
# ferrari, but it now has the `drive` method anyway. This is good, but
# doesn't *quite* match the naming
ferrari drive

# You can also retrieve slots (methods or values), which walks up the heirarchy if necessary
ferrari getSlot("drive") println
# And you can find out what its prototype is
# (i.e. what it inherited from and what exists)
ferrari proto println
# But what if we add something to the ferrari? that isn't covered yet
ferrari name := "My Fast Car"
ferrari proto println
ferrari name println
# It seems to only cover the parent stuff

# There's also a "master namespace" that all the objects get created in:
Lobby println