TIP #257: Object Orientation for Tcl, is the basis for
TclOO which first appeared in Tcl
8.6. Some content on this page is historical; the final implementation differs somewhat from what is described below.
See Also edit
- TIP #257 Discussion
- as it occurred during the development of TclOO
Description edit
The feature set is to be based mostly on
XOTcl, but with the addition of features for improved support for being the core of
Snit. It should also be usable on its own.
Implementation Notes edit
DKF 2006-08-19: There is now a partially-working implementation for people to test out. It is at about the point where you can create an object, give it some methods, and run the methods. However, constructors, destructors and inheritance do not yet work.
To get the source code, you need to check out the branch of CVS that I'm using for the development. You want to get from the usual tcl repository, but you want the branch
tip-257-implementation-branch. Thus, assuming you've just got anonCVS access, you do this (the anon password is empty in case you're wondering, and the second line is split):
cvs -d:pserver:[email protected]:/cvsroot/tcl login
cvs -d:pserver:[email protected]:/cvsroot/tcl -z9 co \
-r tip-257-implementation-branch -d tcl-and-tip257 tcl
Building on both Unix and Win should be supported (no idea about OSX). There is a (small) test file in the Tcl test suite, oo.test. If you want to know to basically use the code, look there first as the code passes the test now. No proper docs yet; use the TIP itself there for the moment.
Example session:
% oo::object create foo
::foo
% oo::define foo {method bar {} {puts "Hello, World!"}}
% foo
wrong # args: should be "foo method ?arg ...?"
% foo ?
unknown method "?": must be bar or destroy
% foo bar
Hello, World!
% oo::define foo {method spong {} {my bar; next}}
% foo spong
Hello, World!
no superclass method implementation
% set errorInfo
no superclass method implementation
while executing
"next"
(method "spong" line 1)
invoked from within
"foo spong"
DKF 2006-08-20: Constructors now work (but there's only one meaningful class to use them on). On the down side, now know that the
global command doesn't work in methods and constructors properly...
(now fixed)DKF 2006-08-21: Destructors, method forwarding (which used to crash), export and unexport now all added. There seems to be some kind of intermittent crash in the destructor code, probably due to memory not being managed quite right, but it doesn't crash on my home machine so tracking it down is slightly awkward. Export and unexport work, but won't work once we get inheritance in, especially in the MI case. Need to rethink how to implement them. :-(
OK, the destructor crash has been pinned down as being due to destructors firing when the OO system as a whole is going away. That degree of cleanup isn't something I've attempted to do yet.
DKF 2006-08-23: Single inheritance now working (or a good approximation of it; it's passing several tests).
RLH: Is it the intention then of only allowing single inheritance and not multiple to avoid the "diamond" effect of inheritance? Or is this just a step in that direction?
DKF 2006-08-24: Multiple inheritance (diamond effects resolved using
XOTcl rules) now working. Still got filters and mixins to do, but they're simple extensions of MI. (Does this answer your question, RLH? :-))
RLH Indeed it does. : )
DKF 2006-08-26: Most of object and class teardown now implemented. Memory leaks should be largely (but not yet completely) banished.
DKF 2006-08-27: Teardown now done, allowing more of test suite to pass. Filters now supported on objects (not class-wide).
DKF 2006-08-28 Per-object mixins and changing-an-object's-class now done. Mainly introspection facilities left in the C part of the development of this functionality.
RLH You *are* sleeping I hope. : )
DKF 2006-08-29 (no, 29 Aug; it's late): Partial implementation of object cloning. Method cloning not working right, and class cloning completely wrong. (And I do this instead of watching television, which is why I have so much time.)
RLH I haven't had television for 12 years now. : )
DKF 2006-08-29: Fixed crashes that were stopping the test suite from passing on Linux. They were caused by trying to run destructors in an interpreter which was cleaning up (a
bad thing!), what happens when an object is deleted the "wrong way round" and the complex tangle of what happens when the object system core goes away. Thanks to
dgp for helping me debug this.
DKF 2006-08-30: Added the start of the introspection facilities called for in the TIP.
DKF 2006-08-31: Non-class object cloning done.
DKF 2006-09-02: Class cloning done, and
oo::definer metaclass implemented. All current tests pass, so what's left are believed to be sins of omission.
DKF 2006-09-17: Nearly finished the
oo::struct class.
DKF 2006-09-18: I believe
oo::struct is now finished (there are some really ugly cases in there!) but still need to write more tests. Also
namespace exported all public oo commands.
DKF 2006-09-20:
oo::struct now done, along with a tested (and much more efficient)
variable method.
DKF: 2006-09-25: Added support for class filters. Removed parameters as they're trivial to add using a script. Finished defining something for
self; the semantics are not exactly as in the original TIP because I found that the meaning of some parts was not so clear. Only significant missing feature is "class mixins", which I don't understand and don't currently propose to implement.
DKF: 2006-10-01: Discovered that class filters don't work how they are documented to work; need to figure out how I think they
should work before deciding whether to change the code or the spec. On the plus side, there's a C API now (though the functions are currently declared in tclOO.h since I don't wish to tinker with the stubs table in a branch) and everything apart from the class filter stuff is now consistent with the TIP.
DKF 2006-10-04: Completed introspection; the code is now
fully introspectable (which it wasn't before, ho hum.)
DKF 2006-10-08: Following experiments into what the
XOTcl Method Traversal Order really is, I've added class mixins. Implementation probably isn't perfect, but this is now a functionally-complete OO core.
DKF 2006-10-21: Code quality now much better. The C API is expanding in size to something much closer to production-ready, and it is now also much easier to plug in OO systems on top. In particular, there is now a (C API only) metadata mechanism for both classes and objects, and the introspection code is now a full ensemble, allowing extra bits and pieces to be plugged in there. Also, the code to copy an object is now its own command,
oo::copy, since it didn't fit well with
oo::define but I don't want it to be a method in itself (copying an object or class may require careful consideration of what this means for the object in question.)
MJ: I am playing around with the new functionality to get a feel for it. To do this I am porting some code from
XOTcl to tip OO. When using constructors I don't seem to be able to define multiple instance variables. Am I doing something wrong in the code below?
oo::class create Test {
constructor args {
variable a b
set a [lindex $args 0]
set b [lindex $args 1]
}
}
set a [Test new a b]
info object $a vars
# returns a, I would have expected {a b} to be returned
TR:
variable a b would set
b as the value of
$a. I don't think this is what you intended. Did you want this?
variable a [lindex $args 0] b [lindex $args 1]
Just my $0.02
MJ: You are of course correct. Rereading the variable section shows I should have used
my variable instead. However I cannot seem to get this to work at all. Some class and instance variable examples would be very welcome.
::oo::class create Test {
method a {} {
my variable a
set a test
puts $a
}
}
[::Test new] a
variable name "a" illegal: must not contain namespace separator
DKF: Looks like a bug. Not yet fixed. :-(
DKF: D'uh! Had the sense of a test inverted; now fixed.