Root Object
The object named Object is the root object of all objects in Xoscript and provides core language features.
The most frequently sent message is on:do:, which expands the functionalities of an object. This message is received by the root object, which consequently links the specified task to the message, and by doing so expands the derived object.
Catch-all methods
Normally if you send a message that does not correspond with a method in the object, it gets ignored. However, you can specify so-called magic methods or catch-all methods that will get invoked if the message is not understood by the object.
There are 4 catch-all methods available. Each message matches a specific number of arguments.
| Catch-all message | Number of arguments |
|---|---|
| respond: | 0 |
| respond:and: | 1 |
| respond:and:and: | 2 |
| respond:and:and:and: | 3 |
Example:
Example:
>> HTML := Object new.
HTML on: ['respond:'] do: { :tag
Out write: (['<tag>'] tag: tag), stop.
}.
HTML on: ['respond:and:'] do: { :tag :html
Out write: (
['<tag>innerHTML</tag>']
tag: tag - [':'],
innerHTML: html
), stop.
}.
>> html := HTML new.
html body h1: ['header'].
Result:
Programmatic messages
Instead of sending out a message directly to an object, it can also be sent via a variable. To do so, you use the message message:arguments:.
Example:
>> x := 5.
>> y := x message: ['×'] arguments: (List ← 2).
Out write: y, stop.
Result:
[ Object ] type
Example:
>> x := Object.
Out write: x type, stop.
Out write: 777 type, stop.
Out write: ['ABC'] type, stop.
Out write: True type, stop.
Result:
[ Object ] code.
Example:
>> x := Dict new.
x abc: 123.
>> y := List ← x.
Out write: y code, stop.
Result:
[ Object ] string
Example:
>> x := Object.
Out write: x string, stop.
Result:
[ Object ] number
Example:
>> x := Object.
Out write: x number, stop.
Result:
[ Object ] boolean
Example:
>> x := Object.
Out write: x bool, stop.
Result:
[ Object ] equals: [ Object ]
Example:
>> x := Object new.
>> y := Object new.
>> z := x.
Out write: ( x equals: y ), stop.
Out write: ( x equals: z ), stop.
Out write: ( x = y ), stop.
Out write: ( x = z ), stop.
Out write: ( x ≠ y ), stop.
Out write: ( x ≠ z ), stop.
Result:
[ Object ] !=: [ Object ]
Example:
>> x := Object new.
>> y := Object new.
>> z := x.
Out write: ( x equals: y ), stop.
Out write: ( x equals: z ), stop.
Out write: ( x = y ), stop.
Out write: ( x = z ), stop.
Out write: ( x ≠ y ), stop.
Out write: ( x ≠ z ), stop.
Result:
[ Object ] myself
Example:
Out write: Object myself, stop.
Out write: True bool, stop.
Out write: 123 number, stop.
Out write: ['x'] string, stop.
Result:
[ Object ] recursive
Example:
>> r := Object new.
r on: ['∞'] do: { :x
Out write: x, stop.
(x < 3) true: { self recursive ∞ (x + 1). }.
}.
r ∞ 1.
Result:
[ Object ] do
Example:
>> x := List ← 1 ; 2 ; 3.
x do pop shift done.
Out write: x, stop.
Result:
[ Object ] done
Example:
>> x := List ← 1 ; 2 ; 3.
x do pop shift done.
Out write: x, stop.
Result:
[ Object ] copy
Example:
>> x := 1.
>> y := x.
>> z := x copy.
x add: 1.
Out write: x, stop.
Out write: y, stop.
Out write: z, stop.
Result:
[ Object ] case: [ Object ] do: [ Code ]
Example:
>> x := ['**'].
x
case: ['*'] do: { Out write: 1. },
case: ['**'] do: { Out write: 2. },
case: ['***'] do: { Out write: 3. }.
Out stop.
Result:
[ Object ] message: [ String ] arguments: [ List ]
Example:
>> x := 5.
>> y := x message: ['×'] arguments: (List ← 2).
Out write: y, stop.
Result:
[ Object ] on: [ String ] do: [ Code ]
Example:
>> q := Object new.
q on: ['abc:'] do: { :p
own abc := p.
}.
q on: ['abc'] do: {
<- own abc.
}.
>> a := q new abc: ['123'].
Out write: a abc, stop.
Result:
[ Object ] respond: [ String ]
Example:
>> x := Object new.
x on: ['respond:'] do: { :a
<- (a + ['!']).
}.
Out write: x abc.
Result:
[ Object ] respond: [ String ] and: [ String ]
Example:
>> repeater := Object new.
repeater on: ['respond:'] do: { :what
Out write: what + [' '].
}.
repeater on: ['respond:and:'] do: { :what :say
Out write: what, write: say + [' '].
}.
repeater on: ['respond:and:and:'] do: { :what :say :more
Out write: what, write: say, write: more.
}.
repeater repeat.
repeater everything: ['I say.'].
repeater and: ['I '] everything: ['do.'].
Result:
[ Object ] None?
Example:
Out write: Object None?, stop.
Result:
[ Object ] learn: [ String ] means: [ String ]
Example:
Number learn: ['-'] means: ['+'].
Out write: 2 - 1, stop.
Result: