1
<HTML><HEAD><TITLE>Section 3: The language of objects</TITLE></HEAD>
2
<BODY BGCOLOR="#FFFFFF">
4
<TR><TD Valign="top"><A HREF="contents.html">Contents</A><BR><A HREF="section2.html">Back</A><BR><A HREF="chapter2.html">Forward</A><TD bgcolor="#F5DEB3"><BLOCKQUOTE><H3>3. The language of objects</H3></BLOCKQUOTE><TR><TD><TD>
8
Objects make up the substance of the world. That is why
9
they cannot be composite.
10
<P>...Ludwig Wittgenstein (<B>1889</B>--<B>1951</B>), <I>Tractatus</I></BLOCKQUOTE>
15
<HR><BLOCKQUOTE><H3>3.1. Objects and communication</H3></BLOCKQUOTE><P>
16
The objects in a program are its constituent parts: little lumps of code
17
and data. The starting point of an "object-oriented language'' is that
18
it's good design to tie up pieces of information in bundles with the pieces
19
of program which deal with them. But the idea goes further:
21
<P>1. -- An object is something you can communicate with. (It's like a
22
company where many people work in the same building, sharing the same
23
address: to the outside world it behaves like a single person.)
24
<P>2. -- Information inside the object can be kept concealed from the
25
outside world (like a company's confidential files). This is sometimes
26
called "encapsulation''.
27
<P>3. -- The outside world can only ask the object to do something, and
28
has no business knowing how it will be done. (The company might decide to change
29
its stock-control system one day, but the outside world should never even
30
notice that this has happened, even though internally it's a dramatic shift.)
33
<P> All three principles have been seen already for routines:
34
(1) you can call a routine, but you can't call "only this part of a routine'';
35
(2) the local variables of a routine are its own private property, and the
36
rest of the program can't find out or alter their values; (3) as long as the
37
routine still accomplishes the same task, it can be rewritten entirely and the
38
rest of the program will carry on working as if no change had been made.
41
Why bother with all this? There are two answers. First and foremost,
42
Inform was designed to make adventure games, where objects are the right
43
idea for representing items and places in the game. Secondly, the 'object'
44
approach makes sense as a way of organising any large, complicated
49
The other key idea is communication. One can visualise the program as being
50
a large group of companies, constantly writing letters to each other to
51
request information or ask for things to be done. In a typical "message'',
52
one object <I>A</I> sends a detailed question or instruction to another object <I>B</I>,
53
which replies with a simple answer. (Again, we've seen this already for
54
routines: one routine calls another, and the other sends back a return value.)
58
Routines are only one of the four basic kinds of Inform object, which are:
60
<P> -- routines, declared using <TT>[</TT>...<TT>]</TT>;
61
<P> -- strings in double-quotes <TT>"like so"</TT>;
62
<P> -- collections of routines and global variables, declared using <TT>Object</TT>;
63
<P> -- prototypes for such collections, called "classes'' and declared using
66
These four kinds are called "metaclasses''. If <TT>O</TT> is an object, then the
72
will always tell you what kind it is, which will be one of the four values
74
Routine String Object Class
79
metaclass("Violin Concerto no. 1")
82
evaluates to <TT>String</TT>, whereas
87
should always be <TT>Routine</TT> (since <TT>Main</TT> should always be the name of the
88
routine where an Inform program begins to run). From <A HREF="section1.html">Section 1</A> we already know
89
about metaclasses <TT>Routine</TT> and <TT>String</TT>, so it's the other two cases which
90
this section will concentrate on.
93
<P><TR><TD Valign="top"><IMG SRC="icons/ddbend.gif" ALT="/\/\"><TD bgcolor="#EEEEEE"><SMALL> Why only these four kinds? Why are strings objects, and not (say)
94
variables or dictionary words? Object-oriented
95
languages vary greatly in to what extreme they take the notion of object: in
96
the dogmatic Smalltalk-80, every ingredient of any kind in a program
97
is called an object: the program itself, the number 17, each variable and
98
so on. Inform is much more moderate. Routines, <TT>Object</TT>s and classes
99
are genuinely object-like, and it just so happens that it's convenient to
100
treat strings as objects (as we shall see). But Inform stops there.
104
<HR><BLOCKQUOTE><H3>3.2. Built-in functions 2: the object tree</H3></BLOCKQUOTE><P>
105
Routines, strings and (as we shall see) classes are scattered about in an
106
Inform program, in no particular order, and nothing links them together.
107
<TT>Object</TT> objects are special in that they are joined up in the "object tree''
108
which grows through every Inform program.
111
In this tree, objects have a kind of family relationship to each other: each
112
one has a parent, a child and a sibling. (The analogy here is with family
113
trees.) Normally such a relation is another object in the tree, but instead
119
which means "no object at all''. For example, consider the tree:
123
Mailbox -> Player
125
Note Sceptre -> Cucumber -> Torch -> Magic Rod
130
The <TT>Mailbox</TT> and <TT>Player</TT> are both children of the <TT>Meadow</TT>, which is their
131
parent, but only the <TT>Mailbox</TT> is <I> the</I> child of the <TT>Meadow</TT>.
132
The <TT>Magic Rod</TT> is the sibling of the <TT>Torch</TT>, which is the sibling of the
133
<TT>Cucumber</TT>, and so on.
137
Inform provides special functions for reading off positions in the tree:
138
<TT>parent</TT>, <TT>sibling</TT> and <TT>child</TT> all do the obvious things, and in addition
139
there's a function called <TT>children</TT> which counts up how many children an
140
object has (where grandchildren don't count as children). For instance,
142
parent ( Mailbox ) == Meadow
143
children ( Player ) == 4
144
child ( Player ) == Sceptre
145
child ( Sceptre ) == nothing
146
sibling ( Torch ) == Magic Rod
150
It is a bad idea to apply these functions to the value <TT>nothing</TT> (since it is
151
not an object, but a value representing the absence of one). One can detect
152
whether a quantity is a genuine object or not using <TT>metaclass</TT>, for
157
is <TT>nothing</TT> for any value <TT>X</TT> which isn't an object: in particular,
159
metaclass(nothing) == nothing
163
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> Hopefully it's clear why the tree is useful for writing adventure
164
games: it provides a way to simulate the vital idea of one thing being
165
contained inside another. But even in non-adventure game programs it can
166
be a convenience. For instance, it is an efficient way to hold
167
tree structures and linked lists of information.
171
<HR><BLOCKQUOTE><H3>3.3. Creating objects 1: setting up the tree</H3></BLOCKQUOTE><P>
172
The object tree's initial state is created with the directive <TT>Object</TT>. For
176
Object -> "starfish" ...
177
Object -> "oyster" ...
178
Object -> -> "pearl" ...
179
Object -> "sand" ...
182
(where the bulk of the definitions are here abbreviated to "<TT>...</TT>''),
183
sets up the tree structure
187
"starfish" --> "oyster" --> "sand"
192
The idea is that if no arrows <TT>-></TT> are given in the <TT>Object</TT> definition, then
193
the object has no parent: if one <TT>-></TT> is given, then the object is a child of
194
the last object to be defined with no arrows; if two are given, then it's a
195
child of the last object defined with only one arrow; and so on. (The list
196
of definitions looks a little like the tree picture turned on its side.)
199
An object definition consists of a "head'' followed by a "body'', which is
200
itself divided into "segments'' (though there the similarity with caterpillars
201
ends). The head takes the form:
203
<TT>Object </TT><I><B><arrows></B></I> <I><B><name></B></I> <TT>"textual name"</TT> <I><B><parent></B></I><BR>
206
but all of these four entries are optional.
207
<P>1. -- The <I><B><arrows></B></I> are as described above. Note that if one or more
208
arrows are given, that automatically specifies what object this is the child
209
of, so a <I><B><parent></B></I> cannot be given as well.
210
<P>2. -- The <I><B><name></B></I> is what the object can be called inside the program;
211
it's analogous to a variable name.
212
<P>3. -- The <TT>"textual name"</TT> can be given if the object's name ever needs
213
to be printed by the program when it is running.
214
<P>4. -- The <I><B><parent></B></I> is an object which this new object is to be a child
215
of. (This is an alternative to supplying arrows.)
217
So much is optional that even the bare directive
222
is allowed, though it makes a nameless and featureless object
223
which is unlikely to be useful.
226
<HR><BLOCKQUOTE><H3>3.4. Statements for objects: move, remove, objectloop</H3></BLOCKQUOTE><P>
227
The positions of objects in the tree are by no means fixed: they are created
228
in a particular formation but are often shuffled around extensively during
229
the program's execution. (In an adventure game, where the objects represent
230
items and rooms, objects are moved in the tree whenever the player picks
231
something up or moves around.) The statement
233
<TT>move </TT><I><B><object></B></I><TT> to </TT><I><B><object></B></I><BR>
236
moves the first-named object to become a child of the second-named one. All
237
of the first object's own children "move along with it'', i.e., remain its own
238
children. For instance, following the example in <A HREF="section3.html">Section 3</A>.2 above,
240
move Cucumber to Mailbox;
247
Mailbox -----------> Player
249
Cucumber -> Note Sceptre -> Torch -> Magic Rod
254
It must be emphasized that <TT>move</TT> prints nothing on the screen, and indeed
255
does nothing at all except to rearrange the tree. When an object becomes the
256
child of another in this way, it always becomes the "eldest'' child in the
257
family-tree sense; that is, it is the new <TT>child()</TT> of its parent, pushing
258
the previous children over into being its siblings.
259
It is, however, illegal to move an object out of such a structure using
261
move Torch to nothing;
264
because <TT>nothing</TT> is not an object as such. The effect is instead achieved with
269
which would now result in
273
Mailbox -----------> Player Battery
275
Cucumber -> Note Sceptre -> Magic Rod
278
So the "object tree'' is often fragmented into many little trees.
282
Since objects move around a good deal, it's useful to be able to test where
283
an object currently is; the condition <TT>in</TT> is provided for this. For
289
is true if and only if the <TT>Cucumber</TT> is one of the <I> direct</I> children
290
of the <TT>Mailbox</TT>. (<TT>Cucumber in Mailbox</TT> is true, but <TT>Cucumber in Meadow</TT>
296
is only an abbreviation for
301
but it's worth having since it occurs so often.
305
The one loop statement missed out in <A HREF="section1.html">Section 1</A> was <TT>objectloop</TT>.
307
<TT>objectloop(</TT><I><B><variable-name></B></I><TT>) </TT><I><B><statement></B></I><BR>
310
runs through the <I><B><statement></B></I> once for each object in the tree, putting each
311
object in turn into the variable. For example,
313
objectloop(x) print (name) x, "^";
316
prints out a list of the textual names of every object in the tree. (Objects
317
which aren't given any textual names in their descriptions come out as "?''.)
318
More powerfully, any condition can be written in the brackets, as long as
319
it begins with a variable name.
321
objectloop(x in Mailbox) print (name) x, "^";
324
prints the names only of those objects which are direct children of the
325
<TT>Mailbox</TT> object.
328
<HR><BLOCKQUOTE><H3>3.5. Creating objects 2: <TT>with</TT> properties</H3></BLOCKQUOTE><P>
329
So far <TT>Object</TT>s are just tokens with names attached which can be shuffled
330
around in a tree. They become interesting when data and routines are
331
attached to them, and this is what the body of an object definition is for.
334
The body contains up to four segments, which can occur in any order; each of
335
the four is optional. The segments are called
337
with has class private
340
<TT>class</TT> will be left until later. The most important segment is <TT>with</TT>,
341
which specifies things to be attached to the object. For example,
343
Object magpie "black-striped bird"
344
with wingspan, worms_eaten;
347
attaches two variables to the bird, one called <TT>wingspan</TT>, the other called
348
<TT>worms_eaten</TT>. Notice that when more than one variable is given, commas are
349
used to separate them: and the object definition as a whole is ended by a
350
semicolon, as always. The values of the magpie's variables are referred to
351
in the rest of the program as
357
which can be used exactly the way normal (global) variables are used. Note
358
that the object has to be named along with the variable, since
360
crested_glebe.wingspan
364
are different variables.
367
Variables which are attached to objects in this way are called "properties''.
368
More precisely, the name <TT>wingspan</TT> is said to be a property, and is said to be
369
"provided'' by both the <TT>magpie</TT> and <TT>crested_glebe</TT> objects.
372
The presence of a property can be tested using the <TT>provides</TT> condition.
375
objectloop (x provides wingspan) ...
378
executes the code <TT>...</TT> for each object <TT>x</TT> in the game which is defined with
379
a <TT>wingspan</TT> property.
382
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> Although the provision of a property can be tested, it cannot be
383
changed while the program is running. The value of <TT>magpie.wingspan</TT> may
384
change, but not the fact that the magpie has a <TT>wingspan</TT>.
389
When the above magpie definition is made, the initial values of
395
are both 0. To create the magpie with a given wingspan, we have to specify an
396
initial value: we do this by giving it after the name, e.g.
398
Object magpie "black-striped bird"
399
with wingspan 5, worms_eaten;
402
and now the program begins with <TT>magpie.wingspan</TT> equal to 5, and
403
<TT>magpie.worms_eaten</TT> still equal to 0. (For consistency perhaps there should be
404
an equals sign before the 5, but if this were the syntax then Inform programs
405
would be horribly full of equals signs.)
408
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> Properties can be arrays instead of global variables. If two or more
409
consecutive values are given for the same property, it becomes an array. Thus,
411
Object magpie "black-striped bird"
412
with name "magpie" "bird" "black-striped" "black" "striped",
413
wingspan 5, worms_eaten;
416
<TT>magpie.name</TT> is not a global variable (and cannot be treated as such: it
417
doesn't make sense to add 1 to it), it is an <TT>--></TT> array. This must be
418
accessed using two special operators, <TT>.&</TT> and <TT>.#</TT>.
423
means "the array which is held in magpie's <TT>name</TT> property'', so that the
424
actual name values are in the entries
426
magpie.&name-->0
427
magpie.&name-->1
429
magpie.&name-->4
432
The size of this array can be discovered with
437
which evaluates to the twice the number of entries, in this case, to 10.
438
(Twice the number of entries because it is actually the number of byte
439
array, <TT>-></TT>, entries: byte arrays take only half as much storage as word
444
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> <TT>name</TT> is actually a special property created by Inform. It has the
445
unique distinction that textual values in double-quotes (like the five words
446
given in <TT>magpie.name</TT> above) are entered into the game's dictionary, and not
447
treated as ordinary strings. (Normally one would use single-quotes for this.
448
The rule here is anomalous and goes back to the misty origins of Inform 1.)
449
If you prefer a consistent style, using single quotes:
451
Object magpie "black-striped bird"
452
with name 'magpie' 'bird' 'black-striped' 'black' 'striped',
453
wingspan 5, worms_eaten;
456
works equally well (except that single-character names like "X'' then have to
457
be written <TT>#n$X</TT>).
461
<P> Finally, properties can also be routines. In the definition
463
Object magpie "black-striped bird"
464
with name "magpie" "bird" "black-striped" "black" "striped",
467
[; return magpie.wingspan + magpie.worms_eaten;
473
<TT>magpie.flying_strength</TT> is neither a variable nor an array, but a routine,
474
given in square brackets as usual. (Note that the Object directive continues
475
where it left off after the routine-end marker, <TT>]</TT>.) Routines which are
476
written in as property values are called "embedded'' and are mainly used
477
to receive messages (as we shall see).
480
<P><TR><TD Valign="top"><IMG SRC="icons/ddbend.gif" ALT="/\/\"><TD bgcolor="#EEEEEE"><SMALL> Embedded routines are unlike ordinary ones in two ways:
481
<P>1. -- An embedded routine has no name of its own, since it is referred to
482
as a property such as <TT>magpie.flying_strength</TT> instead.
483
<P>2. -- If execution reaches the <TT>]</TT> end-marker of an embedded routine,
484
then it returns <TT>false</TT>, not <TT>true</TT> (as a non-embedded routine would). The
485
reason for this will only become clear in Chapter III when <TT>before</TT> and <TT>after</TT>
490
<HR><BLOCKQUOTE><H3>3.6. <TT>private</TT> properties and encapsulation</H3></BLOCKQUOTE><P>
491
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL>
492
An optional system is provided for "encapsulating'' certain properties so
493
that only the object itself has access to them. These are defined by giving
494
them in a segment of the object declaration called <TT>private</TT>. For instance,
496
Object sentry "sentry"
497
private pass_number 16339,
500
if (attempt == sentry.pass_number)
502
"Stand off, stranger.";
507
makes the sentry provide two properties: <TT>challenge</TT>, which is public,
508
and <TT>pass_number</TT>, which can be used only by the sentry's own embedded
513
<P><TR><TD Valign="top"><IMG SRC="icons/ddbend.gif" ALT="/\/\"><TD bgcolor="#EEEEEE"><SMALL> This makes the <TT>provides</TT> condition slightly more interesting than it
514
appeared in the previous section. The answer to the question of whether
517
sentry provides pass_number
520
depends on who's asking: this condition is true if it is tested in one of
521
the sentry's own routines, and otherwise false. A <TT>private</TT> property
522
is so well hidden that nobody else can even know whether or not it exists.
526
<HR><BLOCKQUOTE><H3>3.7. Attributes, <TT>give</TT> and <TT>has</TT></H3></BLOCKQUOTE><P>
527
In addition to properties, objects have flag variables attached. (Recall
528
that flags are variables which are either true or false: the flag is either
529
flying, or not.) However, these are provided in a way which is quite
530
different. Unlike property names, attribute names have to be declared before
531
use with a directive like:
537
Once this declaration is made, every object in the tree has a <TT>tedious</TT> flag
538
attached, which is either true or false at any given time. The state can be
539
tested by the <TT>has</TT> condition:
541
if (magpie has tedious) ...
544
tests whether the magpie's <TT>tedious</TT> flag is currently set, or not.
547
The magpie can be created already having attributes using the <TT>has</TT> segment
550
Object magpie "black-striped bird"
551
with wingspan, worms_eaten
555
The <TT>has</TT> segment contains a list of attributes (with no commas in between)
556
which should be initially set. In addition, an attribute can have a
557
tilde <TT>~</TT> in front, indicating "this is definitely not held''. This is
558
usually what would have happened anyway, but class inheritance (see below)
562
Finally, the state of such a flag is changed in the running of the program
563
using the <TT>give</TT> statement:
568
sets the magpie's <TT>tedious</TT> attribute, and
570
give magpie ~tedious;
573
clears it again. The give statement can take a list of attributes, too:
575
give door ~locked open;
578
for example, meaning "take away <TT>locked</TT> and add on <TT>open</TT>''.
581
<HR><BLOCKQUOTE><H3>3.8. Classes and inheritance</H3></BLOCKQUOTE><P>
583
Having covered routines and strings in <A HREF="section1.html">Section 1</A>, and <TT>Object</TT>s above, the fourth
584
and final metaclass to discuss is that of "classes''. A class is a kind of
585
prototype object from which other objects are copied. These other objects are
586
sometimes called "instances'' or "members'' of the class, and are said
587
to "inherit from'' it.
590
For example, clearly all birds ought to have wingspans, and the property
593
[; return magpie.wingspan + magpie.worms_eaten;
597
(attached to the <TT>magpie</TT> in the example above) is using a formula which should
598
work for any bird. We might achieve this by using directives as follows:
603
[; return self.wingspan + self.worms_eaten;
609
Bird "crested glebe";
616
The first definition sets up a new class called <TT>Bird</TT>. Every example of a
617
<TT>Bird</TT> now automatically provides <TT>wingspan</TT>, a <TT>flying_strength</TT> routine and
618
a count of <TT>worms_eaten</TT>. Note that the four actual birds are created using
619
the <TT>Bird</TT> class-name instead of the usual plain <TT>Object</TT> directive, but this
620
is only a convenient short form for definitions such as:
627
where <TT>class</TT> is the last of the four object definition segments. It's just
628
a list of classes which the object has to inherit from.
631
The <TT>Bird</TT> routine for working out <TT>flying_strength</TT> has to be written in such
632
a way that it can apply to any bird. It has to say "the flying strength of
633
any bird is equal to its wingspan plus the number of worms it has eaten''.
634
To do this, it has used the special value <TT>self</TT>, which means "whatever
635
object is being considered at the moment''. More of this in the next section.
638
Note also that the <TT>Bird</TT> <TT>with</TT> specifies a <TT>wingspan</TT> of 7. This is the value
639
which its members will inherit, unless their own definitions over-ride this,
640
as the magpie and great Auk objects do. Thus the initial position is:
642
Bird Value of wingspan Value of worms_eaten
650
<P><TR><TD Valign="top"><IMG SRC="icons/ddbend.gif" ALT="/\/\"><TD bgcolor="#EEEEEE"><SMALL> In rare cases, clashes between what a class says and what the object
651
says are resolved differently: see <A HREF="section8.html">Section 8</A>.
656
Inform has "multiple inheritance'', which means that any object can inherit
657
from any number of classes. Thus, an object has no single class; rather,
658
it can be a member of several classes at once.
661
Every object is a member of at least one class, because the four "metaclasses''
662
<TT>Routine</TT>, <TT>String</TT>, <TT>Object</TT> and <TT>Class</TT> are themselves classes.
663
(Uniquely, <TT>Class</TT> is a member of itself.) The magpie above is a member of
664
both <TT>Bird</TT> and <TT>Object</TT>.
667
To complicate things further, classes can themselves inherit from other
678
makes <TT>kestrel</TT> a member of both <TT>BirdOfPrey</TT> and of <TT>Bird</TT>. Informally,
679
<TT>BirdOfPrey</TT> is called a "subclass'' of <TT>Bird</TT>.
682
Given all this, it's impossible to have a function called <TT>class</TT>, analogous
683
to <TT>metaclass</TT>, to say what class something belongs to. Instead, there is a
684
condition called <TT>ofclass</TT>:
686
kestrel ofclass Class
691
kestrel ofclass BirdOfPrey
693
kestrel ofclass Object
694
"Canterbury" ofclass String
697
are all true. This condition is especially handy for use with <TT>objectloop</TT>:
699
objectloop (x ofclass Bird) move x to Aviary;
702
moves all the birds to the <TT>Aviary</TT>.
705
<HR><BLOCKQUOTE><H3>3.9. Messages</H3></BLOCKQUOTE><P>
706
That completes the story of how to create objects, and it's time to begin
707
communicating with them by means of messages.
708
Every message has a sender, a receiver and some parameter values
709
attached, and it always produces a reply (which is just a single value). For
712
x = lamp.addoil(5, 80);
715
sends the message <TT>addoil</TT> with parameters 5 and 80 to the object <TT>lamp</TT>, and
716
puts the reply value into <TT>x</TT>. Just as properties like <TT>magpie.wingspan</TT>
717
are variables attached to objects, so messages are received by routines
718
attached to objects, and message-sending is very like making an ordinary
719
Inform function call. The "reply'' is what was called the return value
720
in <A HREF="section1.html">Section 1</A>, and the "parameters'' used to be called function call arguments.
721
But slightly more is involved, as will become apparent.
725
What does the lamp object do to respond to this message? First of all, it
726
must do something. If the programmer hasn't specified an <TT>addoil</TT> routine
727
for the lamp, then an error message will be printed out when the program
728
is run, along the lines of
730
*** The object "lamp" does not provide the property "addoil" ***
733
Not only does <TT>lamp.addoil</TT> have to exist, but it has to hold one of the
734
four kinds of object, or else <TT>nothing</TT>. What happens next depends
735
on the <TT>metaclass</TT> of <TT>lamp.addoil</TT>:
737
<BR><TABLE Border><TR><TD><TT>metaclass</TT> <TD> What happens: <TD> The reply is:
739
<TR><TD><TT>Routine</TT> <TD> the routine is called with the <TD> the routine's return value
740
<TR><TD> <TD> the given parameters
741
<TR><TD><TT>String</TT> <TD> the string is printed, followed <TD> <TT>true</TT>
742
<TR><TD> <TD> by a new-line
743
<TR><TD><TT>Object</TT> <TD> nothing <TD> the object
744
<TR><TD><TT>Class</TT> <TD> nothing <TD> the class
745
<TR><TD><TT>nothing</TT> <TD> nothing <TD> <TT>false</TT>, or 0, or <TT>nothing</TT>
746
<TR><TD> <TD> <TD> (all different ways of writing 0)
750
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> If <TT>lamp.addoil</TT> is a list rather than a single value then the
751
first entry is the one looked at, and the rest are ignored.
757
print kestrel.flying_strength();
760
will print out 15, by calling the <TT>flying_strength</TT> routine provided by the
761
<TT>kestrel</TT> (the same one it inherited from <TT>Bird</TT>), which adds its wingspan of
762
15 to the number of worms it has so far eaten (none), and then returns 15.
763
(You can see all the messages being sent in a game as it runs
764
with the debugging verb "messages'': see <A HREF="section30.html">Section 30</A> for details.)
767
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> For examples of all the other kinds of receiving property, here is
768
roughly what happens when the Inform library tries to move the player northeast
769
from the current room (the <TT>location</TT>) in an adventure game:
771
x = location.ne_to();
772
if (x == nothing) "You can't go that way.";
773
if (x ofclass Object) move player to x;
776
This allows directions to be given with some flexibility in properties like
777
<TT>ne_to</TT> and so on:
779
Object Octagonal_Room "Octagonal Room"
781
ne_to "The north-east doorway is barred by an invisible wall!",
784
[; if (Amulet has worn)
785
{ print "A section of the eastern wall suddenly parts before
786
you, allowing you into...^";
791
[; if (random(5) ~= 1) return Gateway;
792
print "The floor unexpectedly gives way, dropping you through
793
an open hole in the plaster...^";
794
return random(Maze1, Maze2, Maze3, Maze4);
801
Two special variables help with the writing of message routines:
802
<TT>self</TT> and <TT>sender</TT>. <TT>self</TT> always has as value the <TT>Object</TT> which is
803
receiving the message, while <TT>sender</TT> has as value the <TT>Object</TT> which sent it,
804
or <TT>nothing</TT> if it wasn't sent from <TT>Object</TT> (but from some free-standing
805
routine). For example,
808
[; if (~~(sender ofclass CIA_Operative))
809
"Sorry, you aren't entitled to know that.";
815
<HR><BLOCKQUOTE><H3>3.10. Access to superclass values</H3></BLOCKQUOTE><P>
816
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> A fairly common situation in Inform coding is that one has a general
817
class of objects, say <TT>Treasure</TT>, and wants to create an instance of this class
818
which behaves slightly differently. For example, we might have
822
[; if (self provides deposit_points)
823
score = score + self.deposit_points;
824
else score = score + 5;
825
"You feel a sense of increased esteem and worth.";
829
and we want to create an instance called <TT>Bat_Idol</TT> which (say) flutters
830
away, resisting deposition, but only if the room is dark:
832
Treasure Bat_Idol "jewelled bat idol"
834
[; if (location == thedark)
836
"There is a clinking, fluttering sound!";
842
In place of <TT>...</TT>, we have to copy out all of the previous code about
843
depositing treasures. This is clumsy: what we really want is a way of
844
sending the deposit message to <TT>Bat_Idol</TT> but "as if it had not changed the
845
value of deposit it inherited from <TT>Treasure</TT>''. We achieve this with the
846
so-called superclass operator, <TT>::</TT>. (The term "superclass'' is borrowed
847
from the Smalltalk-80 system, where it is more narrowly defined.) Thus, in
848
place of <TT>...</TT>, we could simply write:
850
self.Treasure::deposit();
853
to send itself the <TT>deposit</TT> message again, but this time diverted to the
854
property as provided by Treasure.
858
<TR><TD><TD bgcolor="#EEEEEE"><SMALL>
859
The <TT>::</TT> operator works on all property values, not just for message sending.
862
object.class::property
865
evaluates to the value of the given property which the class would normally
866
pass on (or gives an error if the class doesn't provide that property or if
867
the object isn't a member of that class). Note that <TT>::</TT> exists as an operator
868
in its own right, so it is perfectly legal to write, for example,
870
x = Treasure::deposit; Bat_Idol.x();
873
To continue the avian theme, <TT>BirdOfPrey</TT> might have its
874
own <TT>flying_strength</TT> routine:
877
[; return self.Bird::flying_strength() + self.people_eaten;
881
reflecting the idea that, unlike other birds, these can gain strength by
886
<HR><BLOCKQUOTE><H3>3.11. Philosophy</H3></BLOCKQUOTE><P>
888
<P><TR><TD Valign="top"><IMG SRC="icons/ddbend.gif" ALT="/\/\"><TD bgcolor="#EEEEEE"><SMALL> This section is best skipped until the reader feels entirely happy
889
with the rest of Chapter I. It is aimed mainly at those worried
890
about whether the ideas behind the apparently complicated system of classes and
891
objects are sound. (As Stephen Fry once put it, "Socialism is all very well
892
in practice, but does it work in theory?'') We begin with two definitions:
896
<TR><TD><TD bgcolor="#EEEEEE"><SMALL><P> -- <B> object</B>
897
<P> -- a member of the program's object tree, or a routine in the
898
program, or a literal string in the program. (Routines and
899
strings can't, of course, be moved around in the object tree, but
900
the tests <TT>ofclass</TT> and <TT>provides</TT> can be applied to them, and
901
they can be sent messages.) Objects are part of the compiled
902
program produced by Inform.
904
<P> -- an abstract name for a set of objects in the game, which may have
905
associated with it a set of characteristics shared by its objects.
906
Classes themselves are frequently described by text in the program's
907
source code, but are not part of the compiled program produced by Inform.
910
<P> Here are the full rules:
911
<P>(1) -- Compiled programs are composed of objects, which may have variables
912
attached called "properties''.
913
<P>(2) -- Source code contains definitions of both objects and classes.
914
<P>(3) -- Any given object in the program either is, or is not, a member of
916
<P>(4) -- For every object definition in the source code, an object is made
917
in the final program. The definition specifies which classes this object is
919
<P>(5) -- If an object <TT>X</TT> is a member of class <TT>C</TT>, then <TT>X</TT> "inherits''
920
property values as given in the class definition of <TT>C</TT>.
923
<P> The details of how inheritance takes place are omitted here.
924
But note that one of the things which can be inherited from class <TT>C</TT> is being
925
a member of some other class, <TT>D</TT>.
927
<P>(6) -- For every class definition, an object is made in the final program
928
to represent it, called its "class-object''.
929
<P> For example, suppose we have a class definition like:
935
The class <TT>Dwarf</TT> will generate a class-object in the final program, also
936
called <TT>Dwarf</TT>. This class-object exists in order to receive messages like
937
<TT>create</TT> and <TT>destroy</TT> and, more philosophically, in order to represent
938
the concept of "dwarfness'' within the simulated world.
941
It is important to remember that the class-object of a class is not normally
942
a member of that class. The concept of dwarfness is not itself a dwarf:
943
the condition <TT>Dwarf ofclass Dwarf</TT> is false. Individual dwarves provide
944
a property called <TT>beard_colour</TT>, but the class-object of <TT>Dwarf</TT> does not:
945
the concept of dwarfness has no single beard colour.
947
<P>(7) -- Classes which are automatically defined by Inform are called
948
"metaclasses''. There are four of these: <TT>Class</TT>, <TT>Object</TT>, <TT>Routine</TT>
951
It follows by rule (6) that every Inform program contains the class-objects
952
of these four, also called <TT>Class</TT>, <TT>Object</TT>, <TT>Routine</TT> and <TT>String</TT>.
954
<P>(8) -- Every object is a member of one, and only one, metaclass:
955
<P>(8.1) -- The class-objects are members of <TT>Class</TT>, and no other class.
956
<P>(8.2) -- Routines in the program (including those given as property
957
values) are members of <TT>Routine</TT> and no other class.
958
<P>(8.3) -- Static strings in the program (including those given as property
959
values) are members of <TT>String</TT>, and of no other class.
960
<P>(8.4) -- The objects defined in the source code are members of <TT>Object</TT>,
961
and possibly also of other classes defined in the source code.
963
It follows from (8.1) that <TT>Class</TT> is the unique class whose class-object
964
is one of its own members: the condition <TT>Class ofclass Class</TT> is true,
965
whereas <TT>X ofclass X</TT> is false for every other class <TT>X</TT>.
968
There is one other unusual feature of metaclasses, and it is a rule provided
969
for pragmatic reasons (see below) even though it is not very elegant:
971
<P>(9) -- Contrary to rules (5) and (8.1), the class-objects of the four
972
metaclasses do not inherit from <TT>Class</TT>.
976
This concludes the list of rules. To see what they entail, one needs to
977
know the definitions of the four metaclasses. These definitions are never
978
written out in any textual form inside Inform, as it happens, but here are
979
definitions equivalent to what actually does happen. (There is no such
980
directive as <TT>Metaclass</TT>: none is needed, since only Inform itself can
981
define metaclasses, but the definitions here pretend that there is.)
986
In other words, this is a class from which nothing is inherited. So the
987
ordinary objects described in the source code only have the properties which
988
the source code says they have.
991
with create [; ... ],
992
recreate [ instance; ... ],
993
destroy [ instance; ... ],
994
copy [ instance1 instance2; ... ],
998
So class-objects respond only to these five messages, which are described in
999
detail in the next section, and provide no other properties: <B> except</B> that
1000
by rule (9), the class-objects <TT>Class</TT>, <TT>Object</TT>, <TT>Routine</TT> and <TT>String</TT>
1001
provide no properties at all. The point is that these five messages are
1002
concerned with object creation and deletion at run time. But Inform is a
1003
compiler and not, like Smalltalk-80 or other highly object-oriented
1004
languages, an interpreter. We cannot create the program while it is actually
1005
running, and this is what it would mean to send requests for creation or
1006
deletion to <TT>Class</TT>, <TT>Object</TT>, <TT>Routine</TT> or <TT>String</TT>. (We could write the
1007
above routines to allow the requests to be made, but to print out some error
1008
if they ever are: but it is more efficient to have rule (9) instead.)
1011
with call [ parameters...; ... ];
1014
Routines therefore provide only <TT>call</TT>. See the next section for how to use
1018
with print [; print_ret (string) self; ],
1019
print_to_array [ array; ... ];
1022
Strings therefore provide only <TT>print</TT> and <TT>print_to_array</TT>. See the next
1023
section for how to use these.
1026
To demonstrate this, here is an Inform code representation of what happens
1034
if (~~(O provides M)) "Error: O doesn't provide M";
1036
switch(metaclass(P))
1037
{ nothing, Object, Class: return P;
1038
Routine: return P.call(p1, p2, ...);
1039
String: return P.print();
1043
(The messages <TT>call</TT> and <TT>print</TT> are actually implemented by hand, so this
1044
is not actually a circular definition. Also, this is simplified to remove
1045
details of what happens if <TT>P</TT> is an array.)
1046
</SMALL><TR><TD><TD>
1049
<HR><BLOCKQUOTE><H3>3.12. Sending messages to routines, strings or classes</H3></BLOCKQUOTE><P>
1051
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> In the examples so far, messages have only been sent to proper
1052
<TT>Object</TT>s. But it's a logical possibility to send messages to objects of the
1053
other three metaclasses too: the question is whether they are able to receive
1054
any. The answer is yes, because Inform provides 8 properties for such objects,
1059
<TR><TD><TD bgcolor="#EEEEEE"><SMALL>
1060
The only thing you can do with a <TT>Routine</TT> is to call it. Thus, if <TT>Explore</TT> is
1061
the name of a routine, then
1063
Explore.call(2, 4); and Explore(2, 4);
1066
are equivalent expressions. The message <TT>call(2,4)</TT> means "run this routine
1067
with parameters (2,4)''. This is not quite redundant, because it can be used
1068
more flexibly than ordinary function calls:
1070
x = Explore; x.call(2, 4);
1073
The <TT>call</TT> message replies with the routine's return value.
1076
Two different messages can be sent to a <TT>String</TT>. The first is <TT>print</TT>, which
1077
is provided because it logically ought to be, rather than because it is
1078
useful. So, for example,
1080
("You can see an advancing tide of bison!").print();
1083
prints out the string, followed by a new-line; the <TT>print</TT> message replies
1084
<TT>true</TT>, or 1.
1087
<P><TR><TD Valign="top"><IMG SRC="icons/ddbend.gif" ALT="/\/\"><TD bgcolor="#EEEEEE"><SMALL> <TT>print_to_array</TT> is more useful. It copies out the text of the
1088
string into entries 2, 3, 4, ... of the supplied byte array, and writes the
1089
number of characters as a word into entries 0 and 1. That is, if <TT>A</TT> has
1090
been declared as a suitably large array,
1092
("A rose is a rose is a rose").print_to_array(A);
1095
will cause the text of the string to be copied into the entries
1097
A->2, A->3, ..., A->27
1100
with the value 26 written into
1105
And the reply value of the message is also 26, for convenience.
1109
<TR><TD><TD bgcolor="#EEEEEE"><SMALL>
1110
Five different messages can be sent to objects of metaclass Class, i.e., to
1111
classes, and these are detailed in the next section. (But an exception to this
1112
is that no messages at all can be sent to the four metaclasses <TT>Class</TT>,
1113
<TT>Object</TT>, <TT>Routine</TT> and <TT>String</TT>.)
1114
</SMALL><TR><TD><TD>
1117
<HR><BLOCKQUOTE><H3>3.13. Creating and deleting objects</H3></BLOCKQUOTE><P>
1120
A vexed problem in all object-oriented systems is that it is often elegant
1121
to grow data structures organically, simply conjuring new objects out of
1122
mid-air and attaching them to the structure already built. The problem
1123
is that since resources cannot be infinite, there will come a point
1124
where no new objects can be conjured up. The program must be written so
1125
that it can cope with this, and this can present the programmer with
1126
some difficulty, since the conditions that will prevail when the program
1127
is being run may be hard to predict.
1130
In an adventure-game setting, object creation is useful for something like
1131
a beach full of stones: if the player wants to pick up more and more stones,
1132
the game needs to create a new object for each stone brought into play.
1135
Inform allows object creation, but it insists that the programmer must specify
1136
in advance what the maximum resources ever needed will be: for example, the
1137
maximum number of stones which can ever be in play. Although this is
1138
a nuisance, the reward is that the resulting program is guaranteed to work
1139
correctly on every machine running it (or else to fail in the same way
1140
on every machine running it).
1143
The model is this. When a class is defined, a number <I>N</I> is specified, which
1144
is the maximum number of created instances of the class which the programmer
1145
will ever need at once. When the program is running, "instances'' can be
1146
created (up to this limit); or deleted. One can imagine the class having a
1147
stock of instances, so that creation consists of giving out one of the
1148
stock-pile and deletion consists of taking one back.
1151
Classes can receive the following five messages:
1154
<P><TT>remaining()</TT>
1155
<P> What is the current value of <I>N</I>? That is, how many
1156
more instances can be created?
1157
<P><TT>create()</TT>
1158
<P> Replies with a newly created instance, or
1159
with <TT>nothing</TT> if no more can be created.
1160
<P><TT>destroy(I)</TT>
1161
<P> Destroys the instance <TT>I</TT>, which must previously have
1163
<P><TT>recreate(I)</TT>
1164
<P> Re-initialises the instance <TT>I</TT>, as if it had been
1165
destroyed and then created again.
1166
<P><TT>copy(I, J)</TT>
1167
<P> Copies <TT>I</TT> to be equal to <TT>J</TT>, where both have to be
1168
instances of the class.
1172
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> Note that <TT>recreate</TT> and <TT>copy</TT> can be sent for any instances, not just
1173
instances which have previously been created. For example,
1175
Plant.copy(Gilded_Branch, Poison_Ivy)
1178
copies over all the <TT>Plant</TT> properties and attributes from <TT>Poison_Ivy</TT> to
1179
<TT>Gilded_Branch</TT>, but leaves all the rest alone. Likewise,
1181
Treasure.recreate(Gilded_Branch)
1184
only resets the properties to do with <TT>Treasure</TT>, leaving the <TT>Plant</TT> properties
1189
Unless the definition of a class <TT>C</TT> is made in a special way, <TT>C.remaining()</TT>
1190
will always reply 0, <TT>C.destroy()</TT> will cause an error and <TT>C.create()</TT> will
1191
be refused. This is because the magic number <I>N</I> for a class is normally 0.
1194
The "special way'' is to give <I>N</I> in brackets after the class name. For
1195
example, if the class definition for <TT>Leaf</TT> begins:
1200
then initially <TT>Leaf.remaining()</TT> will reply 100, and the first 100 <TT>create()</TT>
1201
messages will certainly be successful. Others will only succeed if leaves
1202
have been destroyed in the mean time. In all other respects <TT>Leaf</TT> is an
1206
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> Object creation and destruction may need to be more sophisticated
1207
than this. For example, we might have a data structure in which every object
1208
of class <TT>A</TT> is connected in some way with four objects of class <TT>B</TT>. When a
1209
new <TT>A</TT> is created, four new <TT>B</TT>s need to be created for it; and when an <TT>A</TT> is
1210
destroyed, its four <TT>B</TT>s need to be destroyed. In an adventure game setting,
1211
we might imagine that every Dwarf who is created has to carry an Axe of his own.
1215
<TR><TD><TD bgcolor="#EEEEEE"><SMALL>
1216
When an object has been created (or recreated), but before it has been
1217
"given out'' to the program, a <TT>create</TT> message is sent to it (if it provides
1218
<TT>create</TT>). This gives the object a chance to set itself up sensibly.
1219
Similarly, when an object is about to be destroyed, but before it actually is, a
1220
<TT>destroy</TT> message is sent to it (if it provides <TT>destroy</TT>). For example:
1226
[ x; self.beard_colour = random("black", "red", "white", "grey");
1228
! Give this new dwarf an axe, if there are any to spare
1230
x = Axe.create(); if (x ~= nothing) move x to self;
1234
! Destroy any axes being carried by this dwarf
1236
objectloop (x in self && x ofclass Axe) Axe.destroy(x);
1240
</SMALL><TR><TD><TD>
1243
<HR><BLOCKQUOTE><H3>3.14. Footnote on common vs. individual properties</H3></BLOCKQUOTE><P>
1246
<P><TR><TD Valign="top"><IMG SRC="icons/ddbend.gif" ALT="/\/\"><TD bgcolor="#EEEEEE"><SMALL>
1247
The properties used in the sections above are all examples of
1248
"individual properties'', which some objects provide and others do not.
1249
There are also "common properties'' which, because they are inherited
1250
from the class <TT>Object</TT>, are held by every member of <TT>Object</TT>.
1251
An example is <TT>capacity</TT>. The <TT>capacity</TT> can be read for an ordinary
1252
game object (say, a crate) even if it doesn't specify a
1253
<TT>capacity</TT> for itself, and the resulting "default'' value will
1254
be 100. However, this is only a very weak form of inheritance --
1255
you can't change the crate's <TT>capacity</TT> value and the condition
1256
<TT>crate provides capacity</TT> evaluates to <TT>false</TT>.
1258
<TR><TD><TD bgcolor="#EEEEEE"><SMALL>
1259
The properties defined by the Inform library, such as
1260
<TT>capacity</TT>, are all common: mainly because common properties
1261
are marginally faster to access and marginally cheaper on memory.
1262
Only 62 are available, of which the library uses up 48.
1263
Individual properties, on the other hand, are practically
1264
unlimited. It is therefore worth declaring a common property only
1265
in those cases where it will be used very often in your program.
1266
You can declare common properties with the directive:
1268
<TT>Property </TT><I><B><name></B></I><TT>;</TT>
1271
which should be made after the inclusion of "Parser'' but before
1272
first use of the new name. The class <TT>Object</TT> will now
1273
pass on this property, with value 0, to all its members. This
1274
so-called "default value'' can optionally be specified. For
1275
example, the library itself makes the declaration
1277
<TT>Property capacity 100;</TT>
1280
which is why all containers in a game which don't specify any particular
1281
<TT>capacity</TT> can hold up to 100 items.
1282
</SMALL><TR><TD><TD>
1284
<HR><A HREF="contents.html">Contents</A> / <A HREF="section2.html">Back</A> / <A HREF="chapter2.html">Forward</A> <BR>
1285
<A HREF="chapter1.html">Chapter I</A> / <A HREF="chapter2.html">Chapter II</A> / <A HREF="chapter3.html">Chapter III</A> / <A HREF="chapter4.html">Chapter IV</A> / <A HREF="chapter5.html">Chapter V</A> / <A HREF="chapter6.html">Chapter VI</A> / <A HREF="chapterA.html">Appendix</A><HR><SMALL><I>Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright © Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.</I></SMALL></BODY></HTML>