Lists of things

I have a line in my code that defines a list of things, along with the list’s initial contents, like so–

Wastelist is a list of things that varies.  Wastelist is {the discarded newspaper, the bacon wrapper}.

.I’ve discovered that if I try to define the list with its initial entries, before I define the entries, the compiler throws a problem message, saying ‘‘the discarded newspaper’ isn’t a form of any constant I know’ and suggests I make sure that there are spaces after the commas, etc. Then I moved the list to a spot right after I defined those objects. Now it works perfectly.

Why does it work like this, when I am able to refer to those objects long before I define them in most other instances??

1 Like

Someone far more learned in writing compilers should be able to give you a better answer, but in broad terms when a compiler comes across something it can’t make sense of, it has at least 3 choices

  1. stop compiling and complain
  2. put in a placeholder and mark it as something to come back to that might make sense once the whole source has been read through
  3. make a best guess as to what was intended and be prepared to either stop and complain or come back and fix it if something explanatory but contradictory to its guess turns up later

Any of these can be good strategies depending on the language, circumstances, time and memory constraints and sophistication of the compiler.

One of the decisions the Inform compiler has to make when it first encounters this phrase is whether to create two new things called ‘the discarded newspaper’ and ‘the bacon wrapper’ and add them as entries to Wastelist, or instead to leave the list empty and try to come back and fill it in later. Without knowing a great deal more about the compiler, I couldn’t say for sure why it decides the best option is to do neither but stop and complain here. But I would guess it’s something to do with the complexities of creating lists, or of possibly having to go back and destroy things once created.

1 Like

My memory of this situation is that you have to define the empty list AND the objects in their own right, then separately, you set the list to include those objects when play begins. Here’s an example that compiles:

The Lab is a room.

Wastelist is a list of things that varies.

When play begins:
	now Wastelist is {the discarded newspaper, the bacon wrapper};

the discarded newspaper is in lab.
the bacon wrapper is in lab.

I deliberately defined the objects after mentioning them in the list to show that the order of appearance in the source doesn’t matter once all the elements are in place.

-Wade

1 Like

Thank you both so much.

I had a feeling that filling Wastelist at ‘When play begins’, after defining it would work.