Syntax
Declares a record type: a group of named fields, each with its own type. A record is a value: assigning it to a declared variable makes a deep copy.
TYPE <Name>
<field> [AS <type>] [[<last index>, …]]
LIST <field>.<type>()
MAP <field>.<type>()
ARRAY <field>.<type>(<last index>, …)
[METHOD …]
ENDTYPE
TYPE <Name> <field> [AS <type>] … ENDTYPE
Structure <Name> … EndStructure
<variable> AS <Name> [= <record>]Also written : ENDTYPE, Structure, EndStructure
Parts
| <Name> | The type name. Case does not matter. Two types with the same name are refused. |
|---|---|
| <field> | A field name. Case does not matter, and two fields with the same name are refused. |
| <type> | The field type: integer, float, string, uint8… or another record type, even one declared later. In LIST and MAP fields, a PureBasic letter such as i or s. |
| <last index> | The last valid index of one array dimension, not the number of cells. |
| <variable> | A variable that holds a record of this type. |
| <record> | Another record to copy, or NEW <Name>(…). |
Good to know
- A field with no sigil and no
ASis a whole number, as in AGK and DarkBASIC: storing 2.9 keeps 2. UseAS floatorAS float64for positions and speeds. - The closing word is
ENDTYPE.END TYPE(QBasic, Visual Basic) is refused. The one-line form takes simple fields only; LIST, MAP, ARRAY fields and methods need the long form. - Prefer
ASto the sigils. A fieldname$loses its string type as soon as a plainnameis used anywhere in the program. Avoid one-letter field names amongb a w u l i q f d s c: inside a method,SELF.w = 10is read as a type suffix and the field is never written. - A record variable must be declared with
AS.q.x = 1on an undeclared name compiles, then stops the program: "q is not defined". - Three assignments share instead of copying: into an array cell (
fleet(1) = hero), into a record field (s.pos = v), and into a variable not declaredASthe type. Copy the fields one by one to get an independent record there. a = bon two records asks whether they are the same record, not whether their fields are equal.- Index an array field with one pair of square brackets per dimension:
b.grid[1][2].b.grid(1, 2)stops the program, andb.grid[1, 2]silently reaches another cell. - Do not name a TYPE after a built-in type (
Integer,Int,Long,Float,Byte, or a letter such asCorF). The TYPE is accepted, butx AS Floatthen declares a number, and the record fields and methods are missing when the program runs. - PureBasic
Structure … EndStructureis an alias ofTYPE … ENDTYPE. Its field suffixx.fis refused inside a TYPE: writex AS float.
Example
TYPE Vec x AS float y AS floatENDTYPETYPE Ship name AS string fuel AS integer pos AS VecENDTYPEa AS Shipa.name = "Comet"a.fuel = 80.7a.pos.x = 3b AS Shipb = ab.pos.x = 9Console(a.name + " " + Str(a.fuel)) ' Comet 80Console(Str(a.pos.x) + " " + Str(b.pos.x)) ' 3 9In the manual : Records and methods