Structures inherit the features of objects. Structures are basically prototypes or templates of object in that they pre-define functions and
variables within themselves. This allows the application to define it's own loosely attached variables. Objects like this
can be powerful containers for multiple variables. Just as before, the object pointer (->
) symbol
allows access to the object's internal properties.
Objects are defined using the { } notation as of version 1.6.3:
$obj = {}
Prior to 1.6.3, objects needed to be defined using the new object()
syntax.
$x = {} $x->$y = 10 print($x->$y)
In YASS, objects can be seen as structures without rules. It is important to note that if you are chaining these, you must define each inner variable as an object like so:
$x = {} $x->$y = {} $x->$y->$a = 10 $x->$y->$b = 15 print($x->$y->$a)
These inner values can also be assigned with functions, taking advantage of lambda functions makes this easy:
$x = {} $x->$y = function() print (true) end function $x->$y()
It is important to note that functions cannot be directly assigned to objects but must first be abstracted using lambda functions as above.
Objects became available in version 1.4.1 and cannot be used in versions prior to this.
ZPE 1.6.3 completely redesigned the way objects work underneath the compiler and interpreter to make it possible to perform other operations on objects. For example:
structure person $name = ["Jamie", "Balfour"] $age = 27 $email = "test@jamiebalfour.scot" end structure function main($args) $p = new person() $first = $p->$name[0] $second = $p->$name[1] end function
It should be noted that version 1.5.4 added an alternative syntax for defining un-prototyped objects using the JSON notation:
$person = {forename : "Jamie", surname : "Balfour"}
This was a big update that inlines ZPE with ECMAScript's definition of objects.
Version 1.6.5 also changed the way that the serialisation and object definitions of objects was performed by adding 'native functions' to the object:
$person = {forename : "Jamie", surname : "Balfour"} $person->get_definition() $person->serialize("test.zo")
It is also very important to note that all objects and structures use by reference variable assignment. This means that changing an object variable inside one function affects another.
$v = {name : "Test object", inner : {name : "Test inner object"}} print($v) $x = $v->inner print($x) $x->name = "Test inner object new" print($v->inner->name)
In most cases this is desirable, however an easy way to get around this is to perform a copy on the object.
Serialization
As of version 1.5.2 it is also possible to serialize and deserialize objects and structures using ZPE's object serialise (save) and deserialise (open) functions:
ZPE 1.6.6 changed the way that serialisation works on objects so both syntaxes are shown below:
$x = {} $x->$y = function() print(true) end function $x->$y() //ZPE 1.5.2+ $x.serialise("test.output") //ZPE 1.6.6+ $x->serialise("test.output") $y = deserialise("test.output")
Built-in objects
Version 1.7.4 further improves objects by adding inline access to internal objects and is by far the simplest method of using these internally defined classes. For example, ZPE defines a UIBuilder object internally:
$ui = new UIBuilder() $ui->show()