tcl

TclOO : Use a method from another class


My goal here is to reuse a method from a class A, but I don't want to use all the methods of this class, just one.
I've tried to use these 2 functions mixin or/and superclass inside my new class B, but all methods from class A (variables and perhaps other elements I don't know about...) are also accessible on class B.
My question : It's possible to use just the method you want without copying them all ?


Solution

  • Methods don't exist except when bound to a class (or instance object, if it was created with oo::objdefine); the basic C API doesn't give you a way to make a method that isn't bound (well, it does but only to support making constructors and destructors; unbound methods are in a partially-defined state). That binding is part of why methods really aren't procedures (though they share a lot of things).

    To share a method, put it in its own class (with whatever minimal supporting declarations it needs) and mix it into the classes that want to share it. Or define a forward, if that makes more sense in your situation. Or just copy the definition (the result of info class definition is intended to be easily usable that sort of way).