In code I’ve been writing I’ve found a need to be able to “add properties” – but not in a conventional way.
Normally I’d just add a property to some class in my Xojo code and be off & running. But now I’m needing to be able to define “classes” (They’re not really classes) in a way that they can be added EVEN AFTER the application has been compiled.
So this means I need to be able to added to a text file, that gets read, and a new “property” is available on the “class”. Admittedly a very unusual need.
But this IS possible.
There is a little used method that any class can use to make it so the compiler can “lookup” something that is not defined in the conventional ways. That method is _operator_lookup_. By adding this method to a class the compiler will call it to lookup any “name” on a class that it cannot find using its normal mechanisms. Note that this WILL make it so you no longer get compilation errors. So IF you use operator_lookup you need to make sure you have a means to debug any errors (like a break statement or something in you implementation to catch any typo’s)
With those warnings out of the way how can you make use of this ?
In normal code you might never need to.
However, if you suppose you have a class that is dynamically configured and used like the following :
for each configProperty in classInstance.Configuration
// now use this property in some generic way
// maybe like an inspector that can edit anything
inspector.EditProperty configProperty
next
Then its possible you can add a configuration property that can be manipulated in a generic way and you DO NOT know all of them ahead of time.
Operator_Lookup lets you deal with this.
And it can be both a GETTER and a SETTER.
You’ll find some sample code in a series of posts I made about this topic