Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Official ZPE/YASS documentationRecord structure data type

Official ZPE/YASS documentationRecord structure data type

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.

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:

YASS
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.

YASS
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.

YASS
$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.

YASS
$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:

YASS
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

  
Comments
Feedback 👍
Comments are sent via email to me.