Version 6 of oo::class

Updated 2010-08-15 06:46:28 by dkf

Class of all classes in TclOO. All classes are instances of this class (which is consequently an instance of itself!)

http://www.tcl.tk/man/tcl8.6/TclCmd/class.htm


AMG: Is there a compelling reason why [oo::class new] is hidden?

DKF: Because everyone treats classes as named entities with long lifetimes. Expose it if you want.


AMG: Can I define a destructor for a class? Automatically deleting all subclasses, instances, and "mixees" is good, but I'm working on a project that needs some additional cleanup when a class is destroyed.

DKF: Yes. The simplest technique is to make your class an instance of a subclass of oo::class and attach the destructor to that.

oo::class create class-with-destructor {
    superclass oo::class
    destructor {
        puts "bye!"
    }
}

class-with-destructor create MyClass {
    # ...
}

But if you've already got your class instance, do not despair! You can either inject the destructor through a mixin or by modifying the class instance's superclass (both with oo::objdefine):

# Technique 1
oo::objdefine MyClass mixin class-with-destructor
# Technique 2
oo::objdefine MyClass class class-with-destructor

If you're changing objects' classes, remember that classes cannot be changed to non-classes or vice versa, and both oo::object and oo::class cannot have their class changed (they're special, for the sake of sanity).


See also: