Go to the first, previous, next, last section, table of contents.


Extending A Structure

It is sometimes handy to define a new structure which is just like an existing structure except for having an additional slot or two.

For example, recall our example structure type

[ 'person   'name 'age 'sex | ]defstruct

and suppose that we wish to define a new structure type saint which is just like person except for having an extra field in which to count miracles, which we will initialize to two, that being the qualifying number for a saint:

[ 'saint :include 'person   'miracles :initval 2 | ]defstruct

Now we can do

Stack:
[ :name "Nick" :age 3042 :sex nil :miracles 10 | ]make-saint --> nick
Stack:
nick
Stack: #<a saint>
ls
:name	"Nick"
:age	3042
:sex	nil
:miracles	10
Stack:
nick person?
Stack: t
pop nick saint?
Stack: t
pop nick person-name
Stack: "Nick"
pop nick saint-miracles
Stack: 10
pop nick.name
Stack: "Nick"
pop nick.miracles
Stack: 10

Note that all the old access and predicate functions for type person still work on type saint: In general, you can use a saint anywhere you can use a person, you just have the extra field of miracles available.

You may :include only one structure in a given structure definition -- remember, structures are intended to be simple, cheap datastructures! For fancy stuff, you need objects and generic functions.


Go to the first, previous, next, last section, table of contents.