Hello all! Suppose we want an array of variants which has the nice property of raising an event after any modification, like addition or removal of elements, has occurred to itself. Since the class 'Variant[]' does not have this specific capability but, on the order hand, has every other desired array handling functionality we need, we'll make good use of the OOP philosophy of re-usability and extend it to add our desired functionality.
Our extended class will be called 'VariantArray' and will have an event called "Update" that will be raised after any modification procedure has taken place. Therefore, in its header we so declare: Inherits Variant[] Event Update() We now need to advise[1] the parent's methods intended for modification of the array structure. The idea is rather simple: we supplement a parent's method definition by simply raising the 'Update' event after its completion. Let's use the special method '_put' as our example case. This method allows instances of our class to be used as arrays in assignments. In a hypothetical N-dimensional array we'd have the following signature: Public Sub _put(vVariant As Variant, iIndex1 As Integer, ..., iIndexN As Integer) Where '...' is a meta-syntactical symbol which represents the full list of indexes parameters varying from 2 to N-1. Thus the override function would be: Public Sub _put(vVariant As Variant, iIndex1 As Integer, ..., iIndexN As Integer) Super._put(vVariant, iIndex1, ..., iIndexN) Raise Update() End For an array of non-fixed size we'd have the following signature: Public Sub _put(vVariant As Variant, iIndex1 As Integer, ...) Where the syntactical symbol '...' means the method can take extra arguments. As the result of the definition of an arbitrary argument tuple there is no way the previous syntax for calling the parent's method definition could possibly work, i.e., in the expression Super._put(vVariant, iIndex1, ...) there is no string of syntactical symbols one could replace the meta-syntactical symbol '...' in order to make the parent's '_put' method receive the same argument tuple of the child '_put' method. The only hope to solve this problem is to use the static class 'Param' to access the extra arguments in the form of an array. However, we can't use an ordinary function call to accomplish what we want, because it won't accept an array as a meta-argument. Fortunately that's what the method 'Call' of the 'Object' class provides us. Therefore, being 'PARENT' the meta-syntactical variable whose hypothetical syntactic expression points to the parent's method implementations, we'd use: Object.Call(PARENT, "_put", [vVariant, iIndex1].Insert(Param.All)) Even so, there is no expression one could fit in 'PARENT' that would make the call work as intended. In the previous case of an arbitrary but defined number of arguments we used the 'Super' keyword to reference the parent's method implementation. This worked there because it was a direct and imediate use, and would not work here in place of 'PARENT'. So we are stuck, because to reference the parent's method implementation we need to use a syntax which doesn't allow the use of a non-fixed tuple of arguments, and in order to use a non-fixed tuple of arguments we have to give up our ability of referencing the parent's method implementation. The conclusion is quite clear: it can't be done. My questions are: is there any error with the reasoning given above? If not, is there a workaround? If not, how could we improve Gambas to solve this problem? Irrespective to all of this, do you suggest another approach to accomplish the initial objective of obtaining an array which raises events when modified? What about the general idea of advising methods of an inherited class? In general, how can we make it work? Thank you for your attention. I look forward to your answer. Footnotes: [1] I've borrowed the term "advice" from the GNU Emacs terminology for a very similar concept. Here is what its manual says about it: The "advice" feature lets you add to the existing definition of a function, by "advising the function". This is a cleaner method for a library to customize functions defined within Emacs--cleaner than redefining the whole function." -- ,= ,-_-. =. Bruno Félix Rezende Ribeiro (oitofelix) [0x28D618AF] ((_/)o o(\_)) There is no system but GNU; `-'(. .)`-' GNU Linux-Libre is one of its official kernels; \_/ All software must be free as in freedom;
signature.asc
Description: PGP signature
------------------------------------------------------------------------------ Learn Graph Databases - Download FREE O'Reilly Book "Graph Databases" is the definitive new guide to graph databases and their applications. Written by three acclaimed leaders in the field, this first edition is now available. Download your free book today! http://p.sf.net/sfu/NeoTech
_______________________________________________ Gambas-user mailing list Gambas-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user