Custom Types
Extended custom Types may declare :Long and
:Double Fields, defaults, Method parameters, and Method returns.
See Long and Double.
A custom Type is a named record made up of Fields. Each instance has its own field values, and a custom-Type variable stores a reference to an instance rather than a copy of its data.
Declaration
Type TypeName Field fieldName Field fieldNameWithTag$ End Type
TypeName and every Field name must be valid identifiers. Fields may use the Integer, Float, or String type tags, or the name of another custom Type. A Field without a tag is an Integer.
Type Player Field name$ Field health% Field speed# Field target.Player End Type
All Fields are declared between Type and End Type. In Extended mode, Fields must appear before any Type Methods.
Type annotations and member access
Custom-Type annotations identify the Type of a variable, array element, Field, parameter, or return value. Member access selects a Field from an instance.
| Dialect | Type annotation | Member access |
|---|---|---|
| Classic or Secure | player.Player |
player\health |
| Modern | player:Player |
player.health |
; Classic or Secure Local player.Player Dim players.Player(31) player\health = 100 ; Modern Local player:Player Dim players:Player(31) player.health = 100
The traditional %, #, and $ primitive tags
work in every dialect. See Dialects for the
syntax and compatibility rules.
Creating and assigning instances
player.Player = New Player
New TypeName creates an instance and returns its reference. Integer and Float Fields initially contain zero, String Fields contain an empty string, and custom-Type Fields contain Null. Extended mode can instead apply Field defaults and a Method New constructor.
Assigning a custom-Type value copies its reference. It does not duplicate the instance, so both variables refer to the same Fields:
primary.Player = New Player alias.Player = primary alias\health = 75 ; primary\health is now also 75
Null, comparison, and deletion
Null represents no live instance. An uninitialised custom-Type variable
contains Null. Custom-Type values support only = and
<> comparisons, and the other operand must be the same Type or
Null.
If player <> Null Then Print player\name
Delete reference releases the instance and removes it from its implicit Type list. Deletion invalidates every reference to that instance; those references compare as Null, and attempting to access their Fields in a debug build raises an "Object does not exist" error.
Delete player Delete Each Player
Delete Each TypeName deletes every live instance of the named Type. Object lifetime remains explicit: leaving a scope does not automatically Delete the instances referenced by its local variables.
Implicit Type list
Every custom Type has one implicit ordered list of its live instances. New instances are appended to the end, and deleted instances are removed.
| Form | Result |
|---|---|
First TypeName |
The first instance, or Null when the list is empty. |
Last TypeName |
The last instance, or Null when the list is empty. |
After(reference) |
The next instance, or Null at the end of the list. |
Before(reference) |
The previous instance, or Null at the start of the list. |
Insert value Before target |
Moves value immediately before target. |
Insert value After target |
Moves value immediately after target. |
For value.TypeName = Each TypeName |
Visits the live instances in list order. |
Delete Each TypeName |
Deletes every live instance and empties the list. |
For player.Player = Each Player player\health = player\health + 1 Next firstPlayer.Player = First Player lastPlayer.Player = Last Player If firstPlayer <> Null And lastPlayer <> Null Insert lastPlayer Before firstPlayer EndIf
The Extended Type Lists feature provides separate, explicitly created collections of object references. It does not replace or change the implicit list described here.
Extended Type features
- Type Initialisation adds Field defaults and one Method New constructor.
- Type Methods attach behaviour to a Type.
- Type Lists provide explicit ordered collections of Type or Interface references.
- Interfaces define contracts that custom Types can implement.