Difference between revisions of "LavishScript:Object References and Persistence"
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
'''The following information applies to LavishScript in Inner Space 1.12, builds 5929 and later.''' | '''The following information applies to LavishScript in Inner Space 1.12, builds 5929 and later.''' | ||
− | LavishScript allows certain types of objects to be referenced by a [[ObjectType: | + | LavishScript allows certain types of objects to be referenced by a [[ObjectType:weakref|weakref]], thus allowing various objects to be passed by reference to Functions (including Members, Methods, and Atoms). |
− | Specifically, to be referenced by a [[ObjectType: | + | Note that weakref was previously called persistentref. |
+ | |||
+ | Specifically, to be referenced by a [[ObjectType:weakref|weakref]], object types must be defined as supporting ''persistent'' behavior. The LavishScript engine assumes non-persistent objects have the shortest possible lifetime, whereas when a ''persistent'' object is destroyed, all active references are invalidated by the object itself. | ||
Whether an object type supports persistence can be determined by checking the value of type.PersistentClass (e.g. <tt>${Type[int].PersistentClass}</tt>), where no result (NULL) means no persistence. Many LavishScript types support persistence, including: | Whether an object type supports persistence can be determined by checking the value of type.PersistentClass (e.g. <tt>${Type[int].PersistentClass}</tt>), where no result (NULL) means no persistence. Many LavishScript types support persistence, including: | ||
Line 17: | Line 19: | ||
* [[ObjectType:set|set]] | * [[ObjectType:set|set]] | ||
− | == Using a | + | == Using a weakref object == |
− | + | weakref supports both immediate and late initialization | |
; Immediate | ; Immediate | ||
− | variable | + | variable weakref MyRef = SomeOtherObject |
; Late | ; Late | ||
− | variable | + | variable weakref MyRef |
MyRef:SetReference[SomeOtherObject] | MyRef:SetReference[SomeOtherObject] | ||
− | After | + | After weakref initialization, as long as the referenced object exists it can be accessed directly through MyRef as if it were the original object. Whether the reference is still valid or not can be determined by checking the value of weakref.Reference, where no result (NULL) means no reference. (e.g. <tt>${MyRef.Reference(exists)}</tt>) |
== Passing by reference == | == Passing by reference == | ||
− | Because | + | Because weakref supports immediate initialization, a weakref can be used as a parameter to any Function (including Members, Methods, and Atoms). Additionally, a weakref can be returned from an explicitly-defined Member. |
objectdef someObject | objectdef someObject | ||
{ | { | ||
− | member: | + | member:weakref GetItself(weakref PassedByReference) |
{ | { | ||
return PassedByReference | return PassedByReference | ||
} | } | ||
− | member:int GetSize( | + | member:int GetSize(weakref PassedByReference) |
{ | { | ||
return ${PassedByReference.Size} | return ${PassedByReference.Size} | ||
} | } | ||
− | member:int GetElement( | + | member:int GetElement(weakref PassedByReference, string element_key) |
{ | { | ||
return ${PassedByReference["${element_key.Escape}"]} | return ${PassedByReference["${element_key.Escape}"]} |
Latest revision as of 10:02, 1 June 2020
The following information applies to LavishScript in Inner Space 1.12, builds 5929 and later.
LavishScript allows certain types of objects to be referenced by a weakref, thus allowing various objects to be passed by reference to Functions (including Members, Methods, and Atoms).
Note that weakref was previously called persistentref.
Specifically, to be referenced by a weakref, object types must be defined as supporting persistent behavior. The LavishScript engine assumes non-persistent objects have the shortest possible lifetime, whereas when a persistent object is destroyed, all active references are invalidated by the object itself.
Whether an object type supports persistence can be determined by checking the value of type.PersistentClass (e.g. ${Type[int].PersistentClass}), where no result (NULL) means no persistence. Many LavishScript types support persistence, including:
Using a weakref object
weakref supports both immediate and late initialization
- Immediate
variable weakref MyRef = SomeOtherObject
- Late
variable weakref MyRef MyRef:SetReference[SomeOtherObject]
After weakref initialization, as long as the referenced object exists it can be accessed directly through MyRef as if it were the original object. Whether the reference is still valid or not can be determined by checking the value of weakref.Reference, where no result (NULL) means no reference. (e.g. ${MyRef.Reference(exists)})
Passing by reference
Because weakref supports immediate initialization, a weakref can be used as a parameter to any Function (including Members, Methods, and Atoms). Additionally, a weakref can be returned from an explicitly-defined Member.
objectdef someObject { member:weakref GetItself(weakref PassedByReference) { return PassedByReference } member:int GetSize(weakref PassedByReference) { return ${PassedByReference.Size} } member:int GetElement(weakref PassedByReference, string element_key) { return ${PassedByReference["${element_key.Escape}"]} } } function main() { variable collection:int MyCollection variable someObject SomeObject MyCollection:Set[one,2] echo Size of MyCollection (method 1): ${SomeObject.GetItself[MyCollection].Size} -- expected '1' echo Size of MyCollection (method 2): ${SomeObject.GetSize[MyCollection]} -- expected '1' echo Value of MyCollection[one]: ${SomeObject.GetElement[MyCollection,one]} -- expected '2' }