ZPE officially added in the record data type in ZPE 1.10.5 on April 23rd 2022, and have been supported since Fantastic Fox (May 2022).
Prior implementations were always buggy, didn't make sense, had horrid syntax, or were too similar to other constructs such as the map or object.
The record data structure is officially known as the record structure, and it's use case is similar to that of structures or objects.
One of the key differences however is that record fields must be typed. Structures on
the other hand can have their properties typed or untyped. Another difference is that there is
no scope for fields in a record structure compared with a structure where public
protected
or private
can be
applied to a property.
Inspiration for the design of the record structure came from the C-style of doing things, but I feel it's better suited to this. As a teacher in Scotland, it was also the best way to do it in relation to the SQA's way of doing it.
Since they are similar to structures, I decided the best syntax for
defining them involved both the record
and the structure
keywords:
record structure pupil { string forename = "Jamie", string surname = "Balfour" }
It should be noted that the values in a structure cannot be left unspecified, but in a record structure they can be left with no value at all.
record structure pupil { string forename, string surname }
Using record structures
Record structures, much like structures, are instantiated before their
use. Since it is an instance of a record structure, it is created with the new
keyword
which is also reserved for working with objects and structures.
$pupil2 = new pupil()
Record structures also must be usable in the program. By default record structures provide the standard accessor (get) and setter (set) methods to manipulate a record instance, each automatically tailored to what fields are defined in the record. As of ZPE version 1.11.4 (April 2023) fields can be specified as you would in other languages.
$pupil1.forename = "John" $name = $pupil1.forename
Here is an example of a program that reads in a CSV file, decodes it and then transforms it in to a bunch of records:
record pupil is { string forename, string surname } $pupils = [] $pupil_data = csv_decode(file_get_contents("pupils.csv")) for each($pupil_data as $pupil) $p = new pupil() $p.forename = $pupil[0] $p.surname = $pupil[1] list_add_element(&$pupils, $p) end for