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 AS is a whole number, as in AGK and DarkBASIC: storing 2.9 keeps 2. Use AS float or AS float64 for 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 AS to the sigils. A field name$ loses its string type as soon as a plain name is used anywhere in the program. Avoid one-letter field names among b a w u l i q f d s c: inside a method, SELF.w = 10 is read as a type suffix and the field is never written.
  • A record variable must be declared with AS. q.x = 1 on 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 declared AS the type. Copy the fields one by one to get an independent record there.
  • a = b on 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, and b.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 as C or F). The TYPE is accepted, but x AS Float then declares a number, and the record fields and methods are missing when the program runs.
  • PureBasic Structure … EndStructure is an alias of TYPE … ENDTYPE. Its field suffix x.f is refused inside a TYPE: write x 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 9
In the manual : Records and methods