Jamie Balfour

Welcome to my personal website.

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

Official ZPE/YASS documentationStructures and classes

A structure is a data type in which many inner values can be specified. It provides a framework for all variables and functions.

YASS
structure person
 
$name = null
$age = 0
$email = null
 
function set_values($n, $a, $e)
$name = $n
$age = $a
$email = $e
end function
 
end structure

Instead of the full word structure being used, the keyword struct may be used.

A structure must be declared using the new keyword (or before ZPE version 1.12.8 the copyof keyword also worked).

Variables and internal functions can be accessed using the object pointer literal, ->, as shown below:

YASS
function main($args)
$s = new person()
$t = copyof person()
 
//Run an internal function within a structure
$t->set_values("Jack", 20, "jack@example.com")
//Obtain and print a variable from a structure
print($t->$name)
//Set a variable in a structure
$t->$name = "Joseph"
end function

Prior to version 1.5.3, the object pointer literal was called the pointer literal and was represented by => (fat arrow). The fat arrow was moved to association using maps and associative arrays.

Special methods

Structures may also be constructed with parameters as of version 1.5.2 of ZPE. The important thing is that they have a function named _construct:

YASS
structure person
 
$name = null
$age = 0
$email = null
 
function _construct($n, $a, $e)
$name = $n
$age = $a
$email = $e
end function
 
end structure
 
function main($args)
$p = new person("John", 20, "john@example.com")
end function

YASS
structure Person
 
$name = null
$age = 0
$email = null
 
function _output()
return $name & " " & $age
end function
 
end structure
 

Remember, in ZPE, structures are templates or prototypes for new objects and objects are the instantiated versions of these structures.

There are other special functions that can be used within an object such as the _output or the _compare functions:

YASS
structure Person
 
$name = null
$age = 0
$email = null
 
function _construct($n, $a, $e)
$name = $n
$age = $a
$email = $e
end function
 
function _output()
return this->$name & "(" & this->$email & ")"
end function
 
function _compare($o)
return $o->$name == this->$name
end function
 
end structure
 
function main($args)
$p1 = new Person("John", 20, "john@example.com")
$p2 = new Person("John", 48, "john2@example.com")
 
//Will return true
print($p1 == $p2)
end function

Version 1.6.4 of ZPE added ZPEObjectNativeFunctions as an option which allows functions defined within Java to add native Java-based function calls on objects.

Namespaces

ZPE 1.12.8 added support for namespaces to keep code much better organised.

Languages like Java use this to keep packages tidy and prevent bringing ambiguity into code. For example, assume a library by James Smith is imported that introduces a new structure called HTMLBuilder and the program was using ZPE's own HTMLBuilder structure. The new HTMLBuilder would overwrite the existing one. Instead of overwriting this, James could use a namespace on his structure:

YASS
structure HTMLBuilder
 
namespace jsmith/packages
 
/*
* Functions go here
* ...
*/
 
end structure
 
$builder = new jsmith/packages/HTMLBuilder()

Now when ever he needs to use James' HTMLBuilder, he would just use the full path of the structure.

Inheritance

Object inheritance is a major part of object oriented programming. ZPE performs late object inheritance in that it is done at runtime. This has a slight performance hit but saves over adding more properties to a single structure.

YASS
structure Animal
 
$type = "Animal"
 
function _output()
return this->$type
end function
 
end structure
 
structure Person inherits Animal
 
$name = null
 
//Using the name of the structure as the function turns the function into a constructor as of version 1.8.5
function Person($n)
$name = $n
this->$type = "Human"
end function
 
end structure
 
function main($args)
$p1 = new Person("Johnny Baxter")
$p2 = new Person("Nick Harding")
 
//Will return Human both times
print($p1)
print($p2)
end function

ZPE 1.8.5 also added in the option to use the extends keyword instead of inherits to make it more like Java or C.

ZPE 1.8.5 also added the option to use the class or structure name as a function name to create a constructor as shown in the example above.

Comments
Feedback 👍
Comments are sent via email to me.