//a struct that extends an array is code with the syntactic sugar of a struct
//this means that no extra code is generated
//structs normally generated create, destroy, and onDestroy
struct Hello extends array
//instantiation can be a tricky thing. When a struct is created, you have to ensure
//that the instance returned is unique. Furthermore, when a struct is destroyed,
//you need to reuse that struct's instance to save on memory
//instanceCount is a counter that continues to increase as new instances are created
private static integer instanceCount = 0 //how many Hello's that are created
//recycleCount is a counter that continues to increase as structs are destroyed
//this is to ensure that destroyed instances are used the next time the struct
//is created
private static integer recycleCount = 0 //how many Hello ids that were recycled
//recycle is an array that stored the destroyed instances
private static integer array recycle //an array storing recycled Hello's
//this is the create method, a method that creates unique instances of the struct
//thistype is a keyword that translates into the struct's name
//in this case, thistype would be Hello
//if the struct was named BooYa, thistype would be BooYa
public static method create takes nothing returns thistype
//this is a keyword that refers to the current instance
//static methods are like regular functions, so they don't get a this, which is
//why I was able to declare one
//static methods are called via struct.method()
local thistype this
//first check for any destroyed structs
if (recycleCount > 0) then //if there are Hello's waiting to be reused
//decrease the counter so that you are on the right slot in the array
set recycleCount = recycleCount - 1 //dec counter
//set the current instance to the destroyed struct
set this = recycle[recycleCount] //use it
else
//if there are no destroyed structs, make a new one by increasing
//the instance counter
set instanceCount = instanceCount + 1 //0 is null, so first inc by 1
//set the current instance to the instance counter
set this = instanceCount
endif
//perform creation code here :O
//return the created struct
return this
endmethod
//destroy is called when the struct is to be destroyed
//this is not a static method
//a non static method translates to this (using destroy as an example)
//public static method destroy takes thistype this returns nothing
//the instance of the struct is automatically passed in
//they are called via structInstance.method()
public method destroy takes nothing returns nothing
//remember this is passed in, so it exists!
//store this into the recycle variable
//the first struct destroyed would be recycle[0] = this
set recycle[recycleCount] = this
//increase recycle counter
set recycleCount = recycleCount + 1
//if create was called, recycleCount would be decreased back down to 0
//and recycle[0] would be used
//put destruction code here
endmethod
endstruct