view 2-Io/day1.io @ 30:d43f60e98f39

Complete the car example of objects and types
author IBBoard <dev@ibboard.co.uk>
date Wed, 06 Sep 2017 18:42:44 +0100
parents 9c7af76fdbd0
children 2bbabcbc4802
line wrap: on
line source

# Calling "functions" is sending a "message" (right) to an object (left)
# ↓ object  ↓ message
"Hi ho, Io\n" print

# Object creation is by cloning and modifying root "Object" object
# Convention appears to be that classes start with capitals
Vehicle := Object clone

# ":=" creates a "slot" (which can be a value or a function?), whereas "=" assigns to existing ones
Vehicle description := "A description of our vehicle"
# So this would fail because the slot doesn't exist - we'll repeat it at the end to stop it crashing our program too early!
# Vehicle another_field = "This won't work"

# Fetching values is just calling the function
# (but we need to skip ahead of what's in the book and chain an extra "print" to print to stdout)
Vehicle description print
"\n" print

# In addition to the slot we created, there is a default slot called "type"
# (and presumably some default messages, because slotNames apparently isn't a slot!)
Vehicle slotNames print
"\n" print
Vehicle type print
"\n" print
Object type print
"\n" print

# Now we start making a Ferrari as a type of car
Car := Vehicle clone
# Car only directly has one slot
# (Q: How do we find out inhereted slots?)
Car slotNames print
"\n" print
Car type print
"\n" print
# But because of prototype cloning, the description remains the same
Car description print
"\n" print

# Now we make a specifc car
ferrari := Car clone
# But this hasn't gained any slots because it is lower-case
ferrari slotNames print
"\n" print
# It does still have a type, though
ferrari type print
"\n" print

# Types don't mean anything specific - they're just a handy convention
# Variables are case sensitive, so we can make an entire type of Ferraris
Ferrari := Car clone
Ferrari type print
"\n" print
Ferrari slotNames print
"\n" print
# And the lower-case version is unaffected
ferrari slotNames print
"\n" print



Vehicle another_field = "This won't work"