21.2. Descriptions as values

| Chapter 21: Advanced Phrases |

| 21.2. Descriptions as values

In the chapter on Descriptions, we saw that a description can be any source text which describes one or more objects: it might be as simple as "the Corn Market", or as complicated as "open containers which are in dark rooms". More or less the only restriction is that it must be unambiguous as to what counts and what does not: "three containers" is ambiguous as a description because it does not say which three.

We've now seen several interesting tricks with descriptions. In fact, if D is a description, then

> say "You gaze mournfully at [the list of D].";
> let the tally be the number of D;
> let the surprise prize be a random D;
> repeat with item running through D:
> ...

are all standard things to do. These examples make it look as if it must be possible to define phrases which act on descriptions, and in fact it is, because a description can be a value in itself. For example,

> even numbers
> open containers which are in dark rooms

are values of kind "description of numbers" and "description of objects" respectively. In general, if K is any kind then "description of K" is also a kind. Here is how we might make use of that:

> To enumerate (collection - a description of objects):
> repeat with the item running through the collection:
> say "-- [The item]."

This makes "enumerate lighted rooms" run off a list of lighted rooms in a textual format different from the standard one produced by "say the list of lighted rooms". Inside the definition, "collection" can be used wherever a description might be used: here, for instance, we use it as the range for the repeat loop. (That's only possible because the range is limited in size: Inform wouldn't have allowed us to range through, say, all texts.)

Purely as a convenience, we can also write "member of" or "members of" in this context. For instance, in the enumerate definition, it would have been just as good to write "...running through the members of the collection..." in the repeat. (Similarly, we could write "number of members of the collection" or "a random member of the collection", which looks grammatically tidier than writing "number of the collection" or "random of the collection" - though in fact both of these do work.)

Finally, it's sometimes useful in an abstract situation to test

if (value) matches (description of values):

This condition is true if the value matches the description; the kinds must be compatible, or Inform will issue a problem message. There is no point using this for cases where the description is given explicitly: if 4 matches even numbers, ... because it is easier to write just: if 4 is an even number, ... So this condition is only useful when the description is stored in some variable, and its identity is not known.

428
| Example Curare A phrase that chooses and names the least-recently selected item from the collection given, allowing the text to cycle semi-randomly through a group of objects. |
RB

"Curare" A thing has a number called the last use. The last use of a thing is usually 0. Definition: a thing is old if its last use is 12 or less. The actual number chosen in this definition is pretty much irrelevant: the main thing is that we want to establish relative values. The lower the "last use" number of an item, the older that item should be understood to be, as we see here: To decide which thing is cyclically random (collection - a description of objects): let choice be the oldest member of the collection; now the last use of the choice is the turn count; decide on choice. This phrase will select, from the collection of objects passed to it, the one that has been mentioned least recently. This means that if we consult it repeatedly about the same collection, it will begin to cycle predictably; but if new items are added to the collection, it will mention these first before returning to the previous cycle. Now we can use this: After taking inventory: say "You stare morosely at [the cyclically random thing carried by the player], wondering what you're ever going to find to do with it." We could have said "You stare morosely at [the oldest thing carried by the player]" here, but doing so would not have set the "last use" property correctly, so we would not get the cycling behavior that we're looking for. The Evidence Room is a room. Some shelves are scenery supporters in the Evidence Room. A box is a kind of container which is open and not openable. On the shelves is a box. It contains a deformed bullet and a driver's license. The player carries a steel fish hook, a Chinese passport, a tube of synthetic curare, and an envelope full of Euros. Test me with "i / i / i / i / i / i / get all from box / i / i / i".

Previous | Contents | Next