Structs

Structs are named indexed collections of references to values.

Structs have two parts: a type which declares the field names and an optional parent type; and an instance of a struct type which has a reference to a value for each field.

Defining Struct Types

Defining struct types is slightly roundabout because some struct types are defined in C so that the C code can create instances of the struct types.

Furthermore, the C code does not need accessor functions as it can access the internals of the struct types directly.

That said, the Idio code does need to have the accessor functions available otherwise it can’t access the struct internals.

From the C perspective, we have defined a struct type and we only need to have Idio define the predicate, an instance constructor and accessors. This is done with calls to define-struct-accessors-only.

From the Idio perspective, we need to define a struct type and then carry on with what the C struct types do.

So define-struct takes the struct type name and creates the struct type and then calls define-struct-accessors-only as is done for the C struct types.

There are slight variations on the above with export-struct and export-struct-accessors-only which define then export the struct instance predicate, constructor and accessors.

Struct Type Predicates

function struct-type? o

test if o is a struct type

Param o:

object to test

Return:

#t if o is a struct type, #f otherwise

function struct-type-isa? st type

assert that struct type st isa a derivative of struct type type

Param st:

struct type to query

Type st:

struct type

Param type:

struct type to compare

Type type:

struct type

Return:

#t/#f

Struct Type Constructors

function make-struct-type name parent fields

create a struct type

Param name:

struct type name

Type name:

symbol

Param parent:

parent struct type

Type parent:

struct type or #n

Param fields:

field names

Type fields:

list of symbol

Return:

struct type

Rtype:

struct type

Struct Type Attributes

function struct-type-name st

return the name of struct type st

Param st:

struct type to query

Type st:

struct type

Return:

struct type name

Rtype:

symbol

function struct-type-parent st

return the parent of struct type st

Param st:

struct type to query

Type st:

struct type

Return:

struct type parent

Rtype:

struct type or #n

function struct-type-fields st

return the fields of struct type st

Param st:

struct type to query

Type st:

struct type

Return:

struct type fields

Rtype:

list of symbols or #n

Struct Instance Predicates

function struct-instance? o

test if o is a struct instance

Param o:

object to test

Return:

#t if o is a struct instance, #f otherwise

function struct-instance-isa? si st

assert that struct instance si isa a derivative of struct type st

Param si:

struct instance to query

Type si:

struct instance

Param st:

struct type to verify

Type st:

struct type

Return:

#t/#f

Struct Instance Constructors

function make-struct-instance st values

create an instance of struct type st assigning values to the struct type’s fields

Param st:

struct type to create

Type st:

struct type

Param values:

values for fields

Type type:

list

Return:

struct instance

Struct Instance Attributes

function struct-instance-type si

return the struct type of struct instance si

Param si:

struct instance to query

Type si:

struct instance

Return:

struct type

Rtype:

struct type

function struct-instance-fields si

return the struct type fields of struct instance si

Param si:

struct instance to query

Type si:

struct instance

Return:

struct type fields

Rtype:

list

function struct-instance-ref si field

return field field of struct instance si

Param si:

struct instance to query

Type si:

struct instance

Param field:

field name

Type field:

symbol

Return:

value

function struct-instance-set! si field v

set field field of struct instance si to v

Param si:

struct instance to modify

Type si:

struct instance

Param field:

field name

Type field:

symbol

Param v:

value

Type v:

value

Return:

#<unspec>

function %struct-instance-ref-direct si st index

return integer field index index of struct instance si

struct instance si is verified as being an instance of struct type st

Param si:

struct instance to query

Type si:

struct instance

Param st:

struct type to verify

Type st:

struct type

Param index:

field index

Type index:

fixnum

Return:

value

function %struct-instance-set-direct! si st index v

set integer field index index of struct instance si

struct instance si is verified as being an instance of struct type st

Param si:

struct instance to query

Type si:

struct instance

Param st:

struct type to verify

Type st:

struct type

Param index:

field index

Type index:

fixnum

Param v:

value

Type v:

value

Return:

#<unspec>

template define-struct name [fields]

define a structure type called name then call define-struct-accessors-only with name and fields

Param name:

structure name

Type name:

symbol

Param fields:

list of field names, defaults to #n

Type fields:

list of symbols, optional

Note

Using this form the structure type has no parent type.

template define-struct-accessors-only name [fields]

define a structure instance constructor, predicate and accessors for structure fields

Param name:

structure name

Type name:

symbol

Param fields:

list of field names, defaults to #n

Type fields:

list of symbols, optional

Note

Using this form:

  • the predicate will be name?

  • the constructor will be make-name

  • the accessors will be

    • name-field

    • set-name-field!

template export-struct name [fields]

call define-struct with name and fields and then export name and call export-struct-accessors-of

Param name:

structure name

Type name:

symbol

Param fields:

list of field names, defaults to #n

Type fields:

list of symbols, optional

template export-struct-accessors-only name [fields]

call define-struct-accessors-only with name and fields and then export the constructor, predicate and field accessor names

Param name:

structure name

Type name:

symbol

Param fields:

list of field names, defaults to #n

Type fields:

list of symbols, optional

Last built at 2024-05-17T06:10:56Z+0000 from 62cca4c (dev) for Idio 0.3.b.6