- There are reasons why it might be better to store an object's data somewhere 'outside' of itself, and not inside its keys:
- To take advantage of the get/set meta-functions every time a key is gotten/set. Ordinarily the get/set meta-functions are only called if a key does not yet exist.
- To avoid clashes relating to keys/methods sharing the same name.
- You may want the majority of keys to be shown during a for loop, but some keys (used to contain raw data or used for calculations) to be omitted from the for loop.
- You want certain keys to be read-only, so that the user doesn't accidentally edit the contents, but you need the keys to be editable by the object class.
- Here is *an* attempt at solving some of these problems, I would be interested in any other solutions that people would have to offer. It makes use of a global object to store data for all objects of a particular custom object class.
Code: Select all
q:: ;objects - store data outside the object
oArray1 := new MyDataStoreClass
oArray1.key := "value"
MsgBox, % oArray1.key
oArray2 := new MyDataStoreClass
oArray2.key := "value"
MsgBox, % oArray2.key
return
global oGlobalStore
MyDataStoreClassInit()
{
static vDummy := MyDataStoreClassInit()
oGlobalStore := {}
}
class MyDataStoreClass
{
__Get(vKey)
{
vAddr := &this
return oGlobalStore[vAddr, vKey]
}
__Set(vKey, vValue)
{
vAddr := &this
if !oGlobalStore.HasKey(vAddr)
oGlobalStore[vAddr] := {}
oGlobalStore[vAddr, vKey] := vValue
return
}
__Delete()
{
vAddr := &this
oGlobalStore[vAddr].Delete(vAddr)
MsgBox, % "object deleted: " vAddr
}
}