Version 11 of oo::class

Updated 2010-08-16 01:17:14 by AMG

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: How do I do this? Sorry, I'm a total TclOO newb. :^)


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 inject the destructor through modifying the class instance's superclass with oo::objdefine:

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). (And this is something where a mixin won't work; mixins are currently ignored for destructor processing, and while maybe that shouldn't be so, it's how things are now.)

AMG: Wait a second, according to [L1 ], "the destructor is called when objects of the class are deleted". I want a destructor for when the class itself is deleted.

DKF: Yes. I know:

% oo::class create class-with-destructor {
    superclass oo::class
    destructor {
        puts "bye!"
    }
}
::class-with-destructor
% oo::class create foo {
  destructor { puts "killing [self]" }
}
::foo
% foo create bar
::bar
% oo::objdefine foo class class-with-destructor
% foo create bar2
::bar2
% bar2 destroy
killing ::bar2
% foo destroy
bye!
killing ::bar

Is that not what you want? (Note that class destructors are processed before instances; guaranteeing order to be the other way is somewhat tricky…)

AMG: No, it's not what I want. I want to schedule code to run when [class-with-destructor destroy] is invoked, or the class is destroyed by other means (e.g. rename to ""). I have external data associated with the class (not the instances), and that external data needs to be cleaned up when the class goes away.


See also: