All perfectly straightforward, because structures can contain arrays as members. But what if the collections within those objects are not all the same size, and indeed the size of the array is not known at the start? Now you're in trouble because an array structure-member has to be declared when the structure is created, so each of the objects ends up with the same size array:
Code: Select all
DIM object{array(SIZE), other, structure, members}
DIM set_of_objects{(HOWMANY)} = object{}
Code: Select all
DIM object{array(MAX_SIZE), actual_size%, other, structure, members}
DIM set_of_objects{(HOWMANY)} = object{}
For a long time I couldn't think of an elegant solution to this, but there is one: rather than storing the array itself in the structure store a pointer to the array instead:
Code: Select all
DIM object{array%%, other, structure, members}
DIM set_of_objects{(HOWMANY)} = object{}
Code: Select all
DEF PROC_create_array(object{}, size%)
PRIVATE temp()
PTR(temp()) = object.array%%
DIM temp(size%)
object.array%% = PTR(temp())
ENDPROC
Code: Select all
DEF PROC_write_to_array(obkect{}, index%, number)
PRIVATE temp()
PTR(temp()) = object.array%%
temp(index%) = number
ENDPROC
Code: Select all
DEF FN_read_from_array(object{}, index%)
PRIVATE temp()
PTR(temp()) = object.array%%
= temp(index%)
A potentially valuable side-effect is that the 'temporary' array, being a conventional array, can have functions like SUM() and MOD() applied which an array within a structure can't:
Code: Select all
DEF FN_sum_array(object{})
PRIVATE temp()
PTR(temp()) = object.array%%
= SUM(temp())