14 Structures

A new Racket structure type is created with scheme_make_struct_type. This creates the structure type, but does not generate the constructor, etc. procedures. The scheme_make_struct_values function takes a structure type and creates these procedures. The scheme_make_struct_names function generates the standard structure procedures names given the structure type’s name. Instances of a structure type are created with scheme_make_struct_instance and the function scheme_is_struct_instance tests a structure’s type. The scheme_struct_ref and scheme_struct_set functions access or modify a field of a structure.

The structure procedure values and names generated by scheme_make_struct_values and scheme_make_struct_names can be restricted by passing any combination of these flags:

When all values or names are returned, they are returned as an array with the following order: structure type, constructor, predicate, first selector, first mutator, second selector, etc., field-independent select, field-independent mutator. When particular values/names are omitted, the array is compressed accordingly.

Scheme_Object*

 

scheme_make_struct_type

(

Scheme_Object* base_name,

 

 

 

 

Scheme_Object* super_type,

 

 

 

 

Scheme_Object* inspector,

 

 

 

 

int num_init_fields,

 

 

 

 

int num_auto_fields,

 

 

 

 

Scheme_Object* auto_val,

 

 

 

 

Scheme_Object* properties,

 

 

 

 

Scheme_Object* guard)

Creates and returns a new structure type. The base_name argument is used as the name of the new structure type; it must be a symbol. The super_type argument should be NULL or an existing structure type to use as the super-type. The inspector argument should be NULL or an inspector to manage the type. The num_init_fields argument specifies the number of fields for instances of this structure type that have corresponding constructor arguments. (If a super-type is used, this is the number of additional fields, rather than the total number.) The num_auto_fields argument specifies the number of additional fields that have no corresponding constructor arguments, and they are initialized to auto_val. The properties argument is a list of property-value pairs. The guard argument is either NULL or a procedure to use as a constructor guard.

Scheme_Object**

scheme_make_struct_names

(

Scheme_Object* base_name,

 

 

Scheme_Object* field_names,

 

 

int flags,

 

 

int* count_out)

Creates and returns an array of standard structure value name symbols. The base_name argument is used as the name of the structure type; it should be the same symbol passed to the associated call to scheme_make_struct_type. The field_names argument is a (Racket) list of field name symbols. The flags argument specifies which names should be generated, and if count_out is not NULL, count_out is filled with the number of names returned in the array.

Scheme_Object**

scheme_make_struct_values

(

Scheme_Object* struct_type,

 

 

Scheme_Object** names,

 

 

int count,

 

 

int flags)

Creates and returns an array of the standard structure value and procedure values for struct_type. The struct_type argument must be a structure type value created by scheme_make_struct_type. The names procedure must be an array of name symbols, generally the array returned by scheme_make_struct_names. The count argument specifies the length of the names array (and therefore the number of expected return values) and the flags argument specifies which values should be generated.

Scheme_Object*

scheme_make_struct_instance

(

Scheme_Object* struct_type,

 

 

int argc,

 

 

Scheme_Object** argv)

Creates an instance of the structure type struct_type. The argc and argv arguments provide the field values for the new instance.

int

 

scheme_is_struct_instance

(

Scheme_Object* struct_type,

 

 

 

 

Scheme_Object* v)

Returns 1 if v is an instance of struct_type or 0 otherwise.

Scheme_Object*

 

scheme_struct_ref

(

Scheme_Object* s,

 

 

 

 

int n)

Returns the nth field (counting from 0) in the structure s.

void

 

scheme_struct_set

(

Scheme_Object* s,

 

 

 

 

int n,

 

 

 

 

Scheme_Object* v)

Sets the nth field (counting from 0) in the structure s to v.