SS 4Nov2004:
This page is deprecated instead check:
Odys object system.
Please add your comments at the end of the page.
Note: I'm looking for suggestions about how to implement such an OOP system in Pure Tcl (8.4) as fast as possible to have an usable prototype.
# The following is a draft for an OOP system for Tcl.
# It's NOT the implementation, it only shows the semantic of the system
# that follows this guide lines:
#
# - To be a simple class-based OOP system with single inheritance.
# - Coherent, classes are objects.
# - Very dynamic:
# * Objects can change class at runtime
# * Classes can be modified a runtime
# * Support for the 'unknown' method of classes
# - To be not unusual without a good reason. Many folks will recognize
# it as a simple class based OOP system at the first look.
# - Sematically similar to SmallTalk.
#
# This is my first draft. Comments are welcome.
#
# I called this object system "DYSO", DYnamic Simple Object system.
# Class definition. Single inheritance.
class toaster {
var toasted
method toast n {
incr toasted $n
}
}
class smartoaster extend toaster {
method toast n {
if {$toaster > 10} {
error "fire!"
}
$self parent toast $n
}
}
class skeleton {
var x 4
var y
classvar foobar 10
method init ...
method free ...
}
set t [new toaster]
$t toast 10
$t toast 20
puts [$t class] ;# returns the class object
puts [$t class name] ;# toaster
$t chclass smarttoaster ;# Change its class
$t class methods; # Returns a list of methods defined in the class
# Everything may change at runtime
$t class setmethod clean {} {
set toasted 0
}
$t class delmethod clean ;# delete the method
$t class args foobar ;# get arguments of method foobar
$t class body foobar ;# get body of method foobar
$t class vars ;# get a list of instance variables
$t class classvars ;# get a list of class variables
$t class addvar x $val ;# Add instance variable x with default value of $val
$t class delvar x ;# Del instance variable x
$t class addclassvar x $val; #Add class variable x with default value of $val
$t class delclassvar x ;# Del class variable x
# Relations between classes may change at runtime
$t class chparent foobarClass
# Supports object cloning
set foo [$t clone]
# Support for costructor/destructor
class file {
var fd
method init filename {
set fd [open $filename]
}
method free {} {
close $fd
}
}
# Support an unknown method that receive calls to methods not defined
# in the object's class or parent classes.
class foobar {
method unknown {name args} {
puts "method '$name' called with args '$args'"
}
}