view 2-Io/day1-methods.io @ 103:98be775c533c default tip

An odd "non-determinism" example from StackOverflow It is clever, but doesn't make much sense as to how it gets its results
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Jul 2019 13:44:13 +0100
parents 4d3769ac447c
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