Over the last 16 months, I have created a script library to test a number of ideas for potential changes to how objects work. I am fairly happy with the results, and have begun integrating the changes. This library script changes objects to be mostly the way I imagine they will be in the final version.
Object.ahk
Summary of changes implemented by the script:
- Separate data and interface: array elements and properties/methods are separated, and generic objects do not have array elements by default and do not support obj[n]. Use obj.%n% (v2.0-a101+) to access dynamic properties/methods.
- Separate methods and properties.
- Separate static and instance members.
- New data types: Array (linear), Map (associative, type- and case-sensitive).
- Meta-functions are replaced with new meta-methods, which work more like normal methods.
- Different enumeration pattern: _NewEnum and Enumerator objects are replaced with __forin and function/closure objects (or an object with Call instead of Next).
- Reduced multi-dimensional array support: Using multiple indices does not automatically create arrays of arrays.
- New exception type hierarchy.
- New error dialog with stack trace, formatting changes and a few shortcut buttons.
- New object model:
- Since static and instance members are separate, each class is split into the class itself (static members) and its prototype.
- Each object (if under control of the library) ultimately inherits from Object.prototype.
- Each class which extends Object is actually an instance of Class (i.e. inherits from Class.prototype).
- Properties and methods can be defined by calling methods on an individual object or a class's prototype.
- Methods are provided to test for the existence of a property or method.
Using the script
Read the included documentation before using the script.
For basic use of the script, #include Object.ahk (including only this file should not interfere with existing code).
To enable alterations to Object(), {}, Array() and [], #include Object.Override.ahk.
To enable alterations to non-object values, #include Object.Values.ahk.
To enable the new error dialog, #include ErrorDialog.ahk (either include it in the auto-execute section or call OnError("ErrorDialog")).
To #include any of these files, do one of the following:
- #Include by full/relative path.
- Place files directly in a Lib folder and use #include <name> without .ahk.
- Place files in Lib\Object and use #include <Object\name> without .ahk.
- When defining a class, make sure it extends Object or a subclass of Object.
- When defining a class, do not place properties or methods directly inside the class. Instead, place them inside nested classes class _instance or class _static as appropriate. The first time the class object is accessed or instantiated, these nested classes are automatically merged. These nested classes act as a substitute for a static keyword which could hypothetically be applied to any property or method (instance members would be defined by default).
- To define a property and method with the same name (not recommended), create the property after the object is created (in __new or __initclass).
- Fixed: Data/array element access should be done through obj.Item[keyOrIndex] instead of obj[keyOrIndex], except with Array, which supports both. Custom collection classes can define an Item property with parameter(s).
- To enumerate properties with for, use for propname, propvalue in new Enumerator(myObject.Properties()). for cannot accept an enumerator function/closure directly with the current implementation. Follow the same pattern for any other explicitly-called enumeration methods (for instance, if someone defines a method that returns a reverse-order enumerator).
- Replace SetCapacity, GetCapacity and GetAddress with a dedicated binary buffer type.
- Fix Array so that setting Length to a larger value does not cause the new last element to incorrectly be marked as having a value set.
- Other forgotten things.