A struct type is declared using a labeled
block. The label is in the format
is the name of the struct; e.g. if you’re creating a complex number struct you might write
The structure of the
block consists of strings in type-name pairs. The end of the definition is notated as
To continue with our complex number example:
@STRUCT_COMPLEX DATA "FLOAT","REAL" DATA "FLOAT","IMAGINARY" DATA "END"
Here, each line is a field declaration. This defines a struct with two floating-point fields: one named
and one named
. The valid type indicators are
for doubles, and
Now that a valid struct has been defined, we can create a variable based on it. The process is simple:
DIM CPX STRUCT "COMPLEX" OUT CPX
The bare (that is, zero length with no type suffix) array
contains the data representing a
struct. Now it can be treated and manipluated as a struct using the library. More specifically, it contains the
) and data representing all
.) This struct is based on the data specified at
. When first created, all fields specify their default value according to type; for
it is 0, for
it is 0.0, and for
it is the empty string.
The method of manipulating fields is rather simple. To set a field’s value, we use
Here the field
(specified as a string in the second argument) in
is set to 1. The same can be done for all fields and all types of fields without fail. Be wary, however, because due to the lack of proper error handling in SmileBASIC,
trying to set a field that doesn’t exist within the struct fails silently.
Getting the value of a field is just as easy. The
function is used to return that field’s value.
Here the value of the field
is obtained and printed. As with
, this function lacks proper error handling.
A nonexistent field will
return 0, regardless of expected type.
To avoid error conditions, the programmer should check struct types for validity when necessary. The main way of doing this is the
This returns true if
is an instance of the struct
, otherwise false. This test checks both against the struct’s internal type and its
(the name and type of its fields.) If these do not match up
with a defined struct type then the check is guaranteed false. Improper tampering of the struct’s internal data may cause this check to fail when it otherwise would be guaranteed to pass.
A struct can be converted to (and then parsed back out of) a single string. This can be used to keep a collection of structs inside a plain string array, or save a struct to a text file.