6.11.3 Structure Extension

You can create a new structure starting with an existing structure and its fields. E.g., if we also want to define floatlist, we can factor out the ...-next field into a general structure list without payload, and then define intlist and floatlist as extensions of list:20

0
  field: list-next ( list -- addr )
constant list ( -- u )

list
  field: intlist-val ( intlist -- addr )
constant intlist ( -- u )

list
  ffield: floatlist-val ( floatlist -- addr )
constant floatlist ( -- u )

Note that in this variant there is no intlist-next nor a floatlist-next, just a list-next; so when you use, e.g., a floatlist, the organization through extension of list is exposed. This may make it harder to refactor things, so you may prefer to also introduce synonyms intlist-next and floatlist-next.

If you prefer to use begin-structure...end-structure, you can do the equivalent definition as follows:

begin-structure list ( -- u )
  field: list-next ( list -- addr )
end-structure

list extend-structure intlist
  field: intlist-val  ( intlist -- addr )
end-structure

list extend-structure floatlist
  ffield: floatlist-val  ( floatlist -- addr )
end-structure
extend-structure ( n "name" – struct-sys n  ) gforth-1.0 “extend-structure”

Start a new structure name as extension of an existing structure with size n.


Footnotes

(20)

This feature is also known as extended records in Oberon.