Dynamic Tables

version 5/140515 by Jesse McGrew

  • Home page
  • Beginning
  • Previous
  • Next

  • Section: Creating New Tables
    This extension also allows us to create new tables at runtime, which can be referred to with table-name variables or properties. For example:
        let T be a new table with columns {speaker, quip} and 10 blank rows;
    After that phrase, "T" is now a table-name variable pointing to a new table, containing two columns and ten rows. Despite the variable's type being called "table-name", the new table has no actual name, and "say T" will show "** Dynamically created table **" instead.
    Note that we have two obligations if we use this syntax. First, we must define the columns ("speaker" and "quip" in this example) in another table in the source code somewhere, so that Inform knows what kinds of value are supposed to go there. We can reuse columns from another table we're already using, or we can define a new table just for this purpose; once the columns have been defined, we can create as many new tables using them as we need.
    Second, we must not lose the value of "T", since it's the only way to refer to the new table. If the table is going to stay in use until the end of the game, we should store it in a global variable or a property. If we only need the table temporarily, we should return its memory when we're done with it by writing:
        deallocate T;
    (Note that "T" will no longer be a valid table-name once it's been deallocated, so it must not be used afterward.)
    To see whether a particular table-name value points to a table that was created this way, we can write:
        if T was dynamically created, [...]
    This syntax is useful when we have a table-name variable that might point to a table defined in the source, or maybe a new table, and so we aren't sure whether it needs to be deallocated when we're done with it. It can also be used to check whether the table was created successfully -- if there wasn't enough memory to create the new table, the phrase will return false.