![]() | Chapter 11: Phrases | ![]() ![]() |
11.3. Pattern matching |
The following are all valid phrases:
award 2 points
award 8 points
award 30 points
but they do not of course have different definitions. Instead, a single definition (internal to Inform, since "award ... points" comes built in) covers these and all similar cases, like so:
To award (some - a number) points: ...
This is a "pattern": it matches the text "award 4 points", and indeed "award four points", but not
award "blue cheese" points;
which results in the following error:
Problem. You wrote 'award "blue cheese" points', but '"blue cheese"' seems to be some text, whereas I was expecting to find a number there.
The number does not need to be a literal constant such as 52, but can be anything which works out to be a number: the name of a number variable, for instance.
The term "(some - a number)" tells Inform that a value is expected here, and also tells it the kind of that value ("number") and what to call this value ("some"). That name is then used in the rest of the definition, whenever the actual number of points is needed. A second example:
To slam shut (box - an open container): say "With great panache, you slam shut [the box].".
Which would not match, for instance, this:
slam shut the black door;
because the black door is not an open container.
Previous | Contents | Next |