Record initializers

Veracity

Developer
Staff member
I just added something to ASH (revision 6277) that I've had on my "to do" list for a while now. ASH has new syntax for creating a record with the various fields initialized as desired.

new <record name> [ ( [[<value>] [, [<value>]]* ] ) ]

You specify the values by the order of the fields in the record definition. (In Common Lisp, you'd call this a "By Order of Arguments constructor", or a "BOA constructor", for short.) You don't have to give a particular value; any you omit is given the default value for the data type of the field.

Here's a sample ASH script:

Code:
record FooRecord {
  string a;
  item b;
  int c;
};

void printit( string title, FooRecord rec )
{
  print( title );
  print( " a = " + rec.a );
  print( " b = " + rec.b );
  print( " c = " + rec.c );
}

// The following allocates a record with default fields
FooRecord temp;
printit( "No initializer", temp );

// The following allocates a record with default fields
temp = new FooRecord;
printit( "Record name only", temp );

// The following allocates a record with default fields
temp = new FooRecord();
printit( "Empty initializer", temp );

// The following allocates a new record with all fields specified
temp = new FooRecord( "description", $item[toast], 5 + 6 );
printit( "Fully specified initializer", temp );

// The following allocates a new record with not all fields specified 
temp = new FooRecord( "no item", , -1 );
printit( "Sparse initializer", temp );

// The following allocates a new record with not all fields specified 
temp = new FooRecord( "Bogon" );
printit( "Short initializer", temp );
 
Top