Difference between revisions of "LavishScript:Object References and Persistence"

From Lavish Software Wiki
Jump to navigation Jump to search
 
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:persistentref|persistentref]], thus allowing various objects to be passed by reference to Functions (including Members, Methods, and Atoms).
+
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:persistentref|persistentref]], 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.
+
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 persistentref object ==
+
== Using a weakref object ==
persistentref supports both immediate and late initialization
+
weakref supports both immediate and late initialization
 
; Immediate
 
; Immediate
  variable persistentref MyRef = SomeOtherObject
+
  variable weakref MyRef = SomeOtherObject
 
; Late
 
; Late
  variable persistentref MyRef
+
  variable weakref MyRef
 
  MyRef:SetReference[SomeOtherObject]
 
  MyRef:SetReference[SomeOtherObject]
  
After persistentref 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 persistentref.Reference, where no result (NULL) means no reference. (e.g. <tt>${MyRef.Reference(exists)}</tt>)
+
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 persistentref supports immediate initialization, a persistentref can be used as a parameter to any Function (including Members, Methods, and Atoms). Additionally, a persistentref can be returned from an explicitly-defined Member.
+
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:persistentref GetItself(persistentref PassedByReference)
+
   member:weakref GetItself(weakref PassedByReference)
 
   {
 
   {
 
       return PassedByReference
 
       return PassedByReference
 
   }  
 
   }  
 
   
 
   
   member:int GetSize(persistentref PassedByReference)
+
   member:int GetSize(weakref PassedByReference)
 
   {
 
   {
 
       return ${PassedByReference.Size}
 
       return ${PassedByReference.Size}
 
   }
 
   }
 
   
 
   
   member:int GetElement(persistentref PassedByReference, string element_key)
+
   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'
}