In TclOO if I want to borrow only particular method from the other class I just copy it with info class definition class method
. But maybe there is more elegant way to do that?
Thank you in advance.
To copy a definition, you basically do exactly that. (I explicitly designed info class definition
to make that easy, even in tricky cases.) It only works for methods implemented using method
, not other types (such as ones writen directly in C).
But you have other options that might suit. Notably:
You can mix a class into another class to bring in the definitions of methods and so on.
oo::define MyClass mixin TheOtherClass
You can forward a method to call another method, or another object's method, or even just any other Tcl command.
# Forward to other method of myself
oo::define MyClass forward foo my bar
# Forward to other object
oo::define MyClass forward foo $theOtherInstance bar
# Forward to arbitrary command
oo::define MyClass forward foo TheBarCommand
They're not suitable for all cases. In particular, classes designed to be used as dynamic mixins probably should be careful with how they bootstrap any state they depend on, and when forwarding you are forwarding to a different entity that may have it's own separate state.