TclOO
Past,Present,Future…
Donal K. Fellows
University of Manchester / Tcl Core Team
Or “What I’ve Been Doing for the Past
Few Years Instead of Watching Bad TV”…
TcLOO: Past
Where it came from, how it was
developed
What is TclOO?
• New Standard Object System
• Part of Tcl 8.6
– Available as extension for Tcl 8.5
oo::classcreateToaster {
variableuseCount
constructor {} { setuseCount 0 }
methodmakeToast {{slices 1}} {
incruseCount
for {seti 0} {$i<$slices} {incri} {
puts"made slice $i from use $useCount"
}
}
}
set t [Toaster new]
$tmakeToast; #  made slice 1 from use 1
10/02/09 3Tcl Conference 2009, Portland Oregon
Why?
• Existing Object Systems had Issues
– Some were too slow
– Some were messy internally
– Some were entangled with class libraries
– All were poorly integrated with Tcl
• Except Tk, which is plain inflexible
• TclOO Exists to be a Small Object System
– But deeply integrated with Tcl
– Allow other OO systems to be built
on top
10/02/09 4Tcl Conference 2009, Portland Oregon
Where Did TclOO Come
From?
• Many Principal Lineages
– Tk
• General style of method calling
– [incr Tcl]
• Way of declaring classes, much syntax
– XOTcl
• Semantics of class system
– Snit
• Support delegation and scriptability
10/02/09 5Tcl Conference 2009, Portland Oregon
Who and When?
• First Glimmerings at Tcl 2003
– Will Duquette and myself
• Also lead to 8.5’s ensembles
• Nearly 2 Years Later (2005)
– Steve Landers, Will and myself wrote spec
• Prompted by Mike Doyle and Jeff Hobbs
• First prototype by me in 2006
– Started getting much useful TCT input
• Full prototype in 2007
• Committed to Tcl in 2008
– Few small things added since with usage
experience
• Overall Gestation was 3-5 Years!
10/02/09 6Tcl Conference 2009, Portland Oregon
Development
• Scripted Prototype
– Very difficult, so scrapped in favour of…
• CVS Feature Branch of Tcl
– Written in C
– Planned for deep integration
– No unrelated changes
10/02/09 7Tcl Conference 2009, Portland Oregon
Key Milestones
1. Initial Specification
2. Working Method Invocation
3. Definition Command
4. Starting the Test Suite
5. Generic Method C API
6. Converting to a Package
7. Doing a Release
10/02/09 8Tcl Conference 2009, Portland Oregon
Lessons Learned…
• Make a Plan
– Define what success is!
• Study the Competition
– Steal their good ideas!
• Be Clean in Development
– No releases until people will be able to run it
– Don’t mix in irrelevant features
• Testing is Vital
10/02/09 9Tcl Conference 2009, Portland Oregon
TclOO: Present
Features, Use and Performance
What does TclOO Offer?
• Powerful Object System
• Small Feature Set
• Stability
• Deep Integration with Tcl
• Can be Driven from C API
• Fast Core
10/02/09 11Tcl Conference 2009, Portland Oregon
The Example Again
oo::classcreateToaster {
variableuseCount
constructor {} {
setuseCount 0
}
methodmakeToast {{slices 1}} {
incruseCount
for {seti 0} {$i< $slices} {incri} {
puts"made slice $i from use $useCount”
}
}
}
set toastie [Toaster new]
$toastiemakeToast; #  made slice 1 from use 1
10/02/09 12Tcl Conference 2009, Portland Oregon
Making our Toaster Fly
with a Mixin
oo::classcreateFlyingObject {
methodtakeOff! {} { … }
methodland {} { … }
methodgetAltitude {} { … }
}
oo::objdefine $toastiemixinFlyingObject
$toastietakeOff!
$toastiemakeToast
10/02/09 13Tcl Conference 2009, Portland Oregon
TclOO Power
• Same Basic Semantic Model as XOTcl
• Single Rooted Multiple Inheritance
– Subclassable Class Objects in Object System
• Mixins (“ad hoc” classes) and Filters
– Enables things like Prototypes and Aspects
• Two Types of Methods
– Procedure-like
– Forwarded/Delegated
10/02/09 14Tcl Conference 2009, Portland Oregon
TclOO Features
• As Few as Possible
– Scriptable and Composable
• Every Object has its own Namespace
– Holds all variables
• Objects can be Renamed
• Objects can be Reclassed
• Definitions by Scripts
– Somewhat similar to [incr Tcl] and Snit definitions
• Introspection Integrated into [info]
10/02/09 15Tcl Conference 2009, Portland Oregon
Scripting: Class Variables
proc::oo::Helpers::classvar {nameargs} {
# Get reference to class’s namespace
setns [infoobjectnamespace[uplevel 1 {selfclass}]]
# Double up the list of variable names
setvs [list $name $name]
foreachv $args {lappendvs $v $v}
# Link the caller’s locals to the class’s variables
tailcallnamespaceupvar $ns {*}$vs
}
10/02/09 Tcl Conference 2009, Portland Oregon 16
Scripting: Class Methods
proc::oo::define::classmethod {name {args ""} {body ""}} {
# Create the method on the class ifthe caller gave
# arguments and body
if {[llength [infolevel 0]] == 4} {
uplevel1 [listselfmethod $name $args $body]
}
# Get the name of the class being defined
setcls [lindex [infolevel -1] 1]
# Make connection to private class “my” command by
# forwarding
tailcallforward $name [infoobjectnamespace $cls]::my$name
}
10/02/09 Tcl Conference 2009, Portland Oregon 17
Stability and Testing
• Test Suite Covers Normal and Error Cases
– Includes checks for various clean teardowns
– Includes checks for leaking memory
• Goal: As Stable and Robust as Tcl
– Should be no unchecked failures ever
10/02/09 18Tcl Conference 2009, Portland Oregon
Production Use of TclOO
• Powers TDBC
– Also show that TclOO supports UML Composition
• Supports itcl-ng
– This is [incr Tcl] in Tcl as contrib. package
– Uses TclOO to provide basic OO framework
• Commercial Uses
– Example: AnsaldoSTS use it in their railway
maintenance support product
10/02/09 19Tcl Conference 2009, Portland Oregon
Tricks in TDBC: Lifetime
• TDBC uses Cunning Lifetime
Management Techniques
– UML Class Composition
• Based on Ownership
– Each Statement owned by
one Connection
– Each ResultSet owned by one
Statement
• Implemented by Placing
Owned into Owner’s
Namespace
– Automatic deletion when
owner goes away
Connection
Statement Statement
ResultSet ResultSet ResultSet
Statement
10/02/09 Tcl Conference 2009, Portland Oregon 20
Tcl Integration
• Available as Package for 8.5
– Thanks to ActiveState for build support
• Part of Tcl 8.6
– Fully supports NRE
• Doesn’t blow C stack
• Can [yield] inside method calls and constructors
• Connection to Tcl Procedure Engine
– Other OO extensions that use TclOO to implement
methods are no longer tightly coupled to Tcl
10/02/09 21Tcl Conference 2009, Portland Oregon
The TclOO C API
• TclOO uses its own C API
– Generic Method Definition Interface
• All standard methods built on top of that
– Construction API
• No destruction API; use existing facilities
– Introspection API
• Information about objects
• Information about current method
• The C API is a Vital Part of TclOO
10/02/09 22Tcl Conference 2009, Portland Oregon
TclOO Performance
• TclOO is Fast
– Fastest Object System for Tcl
• Massive Amount of Caching
– Especially of method interpretations
• In object, class and Tcl_Obj internal representation
– Caches flushed conservatively
• Critical Paths Analysed to Reduce Hot Allocs
– Object creation
– Method call
10/02/09 23Tcl Conference 2009, Portland Oregon
Performance: Basic Call
0
200000
400000
600000
800000
1000000
1200000
1400000
1600000
1800000
Tcl 8.5.2 Tcl 8.6b1.1
Callspersecond
Procedure
TclOO
XOTcl
[incr Tcl]
Snit
Stooop
10/02/09 24Tcl Conference 2009, Portland Oregon
Performance: Stateful Call
0
200000
400000
600000
800000
1000000
1200000
Tcl 8.5.2 Tcl 8.6b1.1
Callspersecond
Procedure
TclOO
XOTcl
[incr Tcl]
Snit
Stooop
10/02/09 25Tcl Conference 2009, Portland Oregon
Performance: Create/Delete
0
20000
40000
60000
80000
100000
120000
140000
160000
180000
200000
Tcl 8.5.2 Tcl8.6b1.1
Callspersecond
TclOO
XOTcl
[incr Tcl]
Snit
Stooop
10/02/09 26Tcl Conference 2009, Portland Oregon
Performance:
Make/Call 10/Del.
0
10000
20000
30000
40000
50000
60000
70000
Tcl 8.5.2 Tcl 8.6b1.1
Callspersecond
TclOO
XOTcl
[incr Tcl]
Snit
Stooop
10/02/09 27Tcl Conference 2009, Portland Oregon
Performance:
Superclass Call
0
100000
200000
300000
400000
500000
600000
700000
Tcl 8.5.2 Tcl 8.6b1.1
Callspersecond
TclOO
XOTcl
[incr Tcl]
Stooop
10/02/09 28Tcl Conference 2009, Portland Oregon
TclOO: Future
Possible future directions
New TclOO Features?
• Garbage Collection
– Only of unrenamed objects from “new” method
– Problematic because it doesn‟t fit Tcl‟s semantics
• Unlikely to break scripts that weren‟t leaking
• Submethods
– More like Tk/Snit
– Very nice to use, but some tricky issues
• How does inheritance work?
– Portable scripts will make method names be
single words
10/02/09 30Tcl Conference 2009, Portland Oregon
Jazzing Up TclOO’s Internals
• Slots
– Better way to manage configuration
• Likely to cause issues with anything named
starting with a “-” character
– But slots are objects
– Should methods and variables be objects?
• Needs investigation
• Poking in the Guts
– e.g., ways to change how methods are looked
up
• Currently some hacks for itcl-ng; want better…
10/02/09 31Tcl Conference 2009, Portland Oregon
Building a Class Library
• Already prototyped on Wiki
– Serialization
– Channel engine
– Megawidgets
• How to distribute between packages?
– Which in Tcl?
– Which in Tk?
– Which in Tcllib?
10/02/09 32Tcl Conference 2009, Portland Oregon
Object Serialization
• Write Objects to a String and
Read Back
– Can also go over sockets or through
files or …
• Experimental Package on Wiki
– http://wiki.tcl.tk/23444
– Does not serialize classes
– Does not deal with object name
clashes
– Needs cooperation from objects to
explore object graph
Interp.
#1
Socket
Interp.
#2
10/02/09 33Tcl Conference 2009, Portland Oregon
Channel Engine Classes
• Classes to Make Writing Channels Easy
– Based on Andreas Kupries‟s channel API
– Both full channels and transformations
• Prototype Code on Wiki
– http://wiki.tcl.tk/21723
– Introspection to find what features to support
10/02/09 34Tcl Conference 2009, Portland Oregon
Megawidgets
• Make Tk Widgets with TclOO Objects
– Work in “the same” way
– Wrap actual widgets
• Prototype Code on Wiki
– http://wiki.tcl.tk/21103
• Already Driven Two Features Added
– The „variable‟ declaration
– Improved method forwarding to object-local
commands
10/02/09 35Tcl Conference 2009, Portland Oregon
Where Next?
• TclOO Intended to be Foundation
– Fast, light, small, stable, and above all Tcl-ish
– It deals with the really complicated bits so you
don‟t have to
• Features to Add Should be Community-
Driven
– If you want it, let us know!
10/02/09 36Tcl Conference 2009, Portland Oregon

TclOO: Past Present Future

  • 1.
    TclOO Past,Present,Future… Donal K. Fellows Universityof Manchester / Tcl Core Team Or “What I’ve Been Doing for the Past Few Years Instead of Watching Bad TV”…
  • 2.
    TcLOO: Past Where itcame from, how it was developed
  • 3.
    What is TclOO? •New Standard Object System • Part of Tcl 8.6 – Available as extension for Tcl 8.5 oo::classcreateToaster { variableuseCount constructor {} { setuseCount 0 } methodmakeToast {{slices 1}} { incruseCount for {seti 0} {$i<$slices} {incri} { puts"made slice $i from use $useCount" } } } set t [Toaster new] $tmakeToast; #  made slice 1 from use 1 10/02/09 3Tcl Conference 2009, Portland Oregon
  • 4.
    Why? • Existing ObjectSystems had Issues – Some were too slow – Some were messy internally – Some were entangled with class libraries – All were poorly integrated with Tcl • Except Tk, which is plain inflexible • TclOO Exists to be a Small Object System – But deeply integrated with Tcl – Allow other OO systems to be built on top 10/02/09 4Tcl Conference 2009, Portland Oregon
  • 5.
    Where Did TclOOCome From? • Many Principal Lineages – Tk • General style of method calling – [incr Tcl] • Way of declaring classes, much syntax – XOTcl • Semantics of class system – Snit • Support delegation and scriptability 10/02/09 5Tcl Conference 2009, Portland Oregon
  • 6.
    Who and When? •First Glimmerings at Tcl 2003 – Will Duquette and myself • Also lead to 8.5’s ensembles • Nearly 2 Years Later (2005) – Steve Landers, Will and myself wrote spec • Prompted by Mike Doyle and Jeff Hobbs • First prototype by me in 2006 – Started getting much useful TCT input • Full prototype in 2007 • Committed to Tcl in 2008 – Few small things added since with usage experience • Overall Gestation was 3-5 Years! 10/02/09 6Tcl Conference 2009, Portland Oregon
  • 7.
    Development • Scripted Prototype –Very difficult, so scrapped in favour of… • CVS Feature Branch of Tcl – Written in C – Planned for deep integration – No unrelated changes 10/02/09 7Tcl Conference 2009, Portland Oregon
  • 8.
    Key Milestones 1. InitialSpecification 2. Working Method Invocation 3. Definition Command 4. Starting the Test Suite 5. Generic Method C API 6. Converting to a Package 7. Doing a Release 10/02/09 8Tcl Conference 2009, Portland Oregon
  • 9.
    Lessons Learned… • Makea Plan – Define what success is! • Study the Competition – Steal their good ideas! • Be Clean in Development – No releases until people will be able to run it – Don’t mix in irrelevant features • Testing is Vital 10/02/09 9Tcl Conference 2009, Portland Oregon
  • 10.
  • 11.
    What does TclOOOffer? • Powerful Object System • Small Feature Set • Stability • Deep Integration with Tcl • Can be Driven from C API • Fast Core 10/02/09 11Tcl Conference 2009, Portland Oregon
  • 12.
    The Example Again oo::classcreateToaster{ variableuseCount constructor {} { setuseCount 0 } methodmakeToast {{slices 1}} { incruseCount for {seti 0} {$i< $slices} {incri} { puts"made slice $i from use $useCount” } } } set toastie [Toaster new] $toastiemakeToast; #  made slice 1 from use 1 10/02/09 12Tcl Conference 2009, Portland Oregon
  • 13.
    Making our ToasterFly with a Mixin oo::classcreateFlyingObject { methodtakeOff! {} { … } methodland {} { … } methodgetAltitude {} { … } } oo::objdefine $toastiemixinFlyingObject $toastietakeOff! $toastiemakeToast 10/02/09 13Tcl Conference 2009, Portland Oregon
  • 14.
    TclOO Power • SameBasic Semantic Model as XOTcl • Single Rooted Multiple Inheritance – Subclassable Class Objects in Object System • Mixins (“ad hoc” classes) and Filters – Enables things like Prototypes and Aspects • Two Types of Methods – Procedure-like – Forwarded/Delegated 10/02/09 14Tcl Conference 2009, Portland Oregon
  • 15.
    TclOO Features • AsFew as Possible – Scriptable and Composable • Every Object has its own Namespace – Holds all variables • Objects can be Renamed • Objects can be Reclassed • Definitions by Scripts – Somewhat similar to [incr Tcl] and Snit definitions • Introspection Integrated into [info] 10/02/09 15Tcl Conference 2009, Portland Oregon
  • 16.
    Scripting: Class Variables proc::oo::Helpers::classvar{nameargs} { # Get reference to class’s namespace setns [infoobjectnamespace[uplevel 1 {selfclass}]] # Double up the list of variable names setvs [list $name $name] foreachv $args {lappendvs $v $v} # Link the caller’s locals to the class’s variables tailcallnamespaceupvar $ns {*}$vs } 10/02/09 Tcl Conference 2009, Portland Oregon 16
  • 17.
    Scripting: Class Methods proc::oo::define::classmethod{name {args ""} {body ""}} { # Create the method on the class ifthe caller gave # arguments and body if {[llength [infolevel 0]] == 4} { uplevel1 [listselfmethod $name $args $body] } # Get the name of the class being defined setcls [lindex [infolevel -1] 1] # Make connection to private class “my” command by # forwarding tailcallforward $name [infoobjectnamespace $cls]::my$name } 10/02/09 Tcl Conference 2009, Portland Oregon 17
  • 18.
    Stability and Testing •Test Suite Covers Normal and Error Cases – Includes checks for various clean teardowns – Includes checks for leaking memory • Goal: As Stable and Robust as Tcl – Should be no unchecked failures ever 10/02/09 18Tcl Conference 2009, Portland Oregon
  • 19.
    Production Use ofTclOO • Powers TDBC – Also show that TclOO supports UML Composition • Supports itcl-ng – This is [incr Tcl] in Tcl as contrib. package – Uses TclOO to provide basic OO framework • Commercial Uses – Example: AnsaldoSTS use it in their railway maintenance support product 10/02/09 19Tcl Conference 2009, Portland Oregon
  • 20.
    Tricks in TDBC:Lifetime • TDBC uses Cunning Lifetime Management Techniques – UML Class Composition • Based on Ownership – Each Statement owned by one Connection – Each ResultSet owned by one Statement • Implemented by Placing Owned into Owner’s Namespace – Automatic deletion when owner goes away Connection Statement Statement ResultSet ResultSet ResultSet Statement 10/02/09 Tcl Conference 2009, Portland Oregon 20
  • 21.
    Tcl Integration • Availableas Package for 8.5 – Thanks to ActiveState for build support • Part of Tcl 8.6 – Fully supports NRE • Doesn’t blow C stack • Can [yield] inside method calls and constructors • Connection to Tcl Procedure Engine – Other OO extensions that use TclOO to implement methods are no longer tightly coupled to Tcl 10/02/09 21Tcl Conference 2009, Portland Oregon
  • 22.
    The TclOO CAPI • TclOO uses its own C API – Generic Method Definition Interface • All standard methods built on top of that – Construction API • No destruction API; use existing facilities – Introspection API • Information about objects • Information about current method • The C API is a Vital Part of TclOO 10/02/09 22Tcl Conference 2009, Portland Oregon
  • 23.
    TclOO Performance • TclOOis Fast – Fastest Object System for Tcl • Massive Amount of Caching – Especially of method interpretations • In object, class and Tcl_Obj internal representation – Caches flushed conservatively • Critical Paths Analysed to Reduce Hot Allocs – Object creation – Method call 10/02/09 23Tcl Conference 2009, Portland Oregon
  • 24.
    Performance: Basic Call 0 200000 400000 600000 800000 1000000 1200000 1400000 1600000 1800000 Tcl8.5.2 Tcl 8.6b1.1 Callspersecond Procedure TclOO XOTcl [incr Tcl] Snit Stooop 10/02/09 24Tcl Conference 2009, Portland Oregon
  • 25.
    Performance: Stateful Call 0 200000 400000 600000 800000 1000000 1200000 Tcl8.5.2 Tcl 8.6b1.1 Callspersecond Procedure TclOO XOTcl [incr Tcl] Snit Stooop 10/02/09 25Tcl Conference 2009, Portland Oregon
  • 26.
    Performance: Create/Delete 0 20000 40000 60000 80000 100000 120000 140000 160000 180000 200000 Tcl 8.5.2Tcl8.6b1.1 Callspersecond TclOO XOTcl [incr Tcl] Snit Stooop 10/02/09 26Tcl Conference 2009, Portland Oregon
  • 27.
    Performance: Make/Call 10/Del. 0 10000 20000 30000 40000 50000 60000 70000 Tcl 8.5.2Tcl 8.6b1.1 Callspersecond TclOO XOTcl [incr Tcl] Snit Stooop 10/02/09 27Tcl Conference 2009, Portland Oregon
  • 28.
    Performance: Superclass Call 0 100000 200000 300000 400000 500000 600000 700000 Tcl 8.5.2Tcl 8.6b1.1 Callspersecond TclOO XOTcl [incr Tcl] Stooop 10/02/09 28Tcl Conference 2009, Portland Oregon
  • 29.
  • 30.
    New TclOO Features? •Garbage Collection – Only of unrenamed objects from “new” method – Problematic because it doesn‟t fit Tcl‟s semantics • Unlikely to break scripts that weren‟t leaking • Submethods – More like Tk/Snit – Very nice to use, but some tricky issues • How does inheritance work? – Portable scripts will make method names be single words 10/02/09 30Tcl Conference 2009, Portland Oregon
  • 31.
    Jazzing Up TclOO’sInternals • Slots – Better way to manage configuration • Likely to cause issues with anything named starting with a “-” character – But slots are objects – Should methods and variables be objects? • Needs investigation • Poking in the Guts – e.g., ways to change how methods are looked up • Currently some hacks for itcl-ng; want better… 10/02/09 31Tcl Conference 2009, Portland Oregon
  • 32.
    Building a ClassLibrary • Already prototyped on Wiki – Serialization – Channel engine – Megawidgets • How to distribute between packages? – Which in Tcl? – Which in Tk? – Which in Tcllib? 10/02/09 32Tcl Conference 2009, Portland Oregon
  • 33.
    Object Serialization • WriteObjects to a String and Read Back – Can also go over sockets or through files or … • Experimental Package on Wiki – http://wiki.tcl.tk/23444 – Does not serialize classes – Does not deal with object name clashes – Needs cooperation from objects to explore object graph Interp. #1 Socket Interp. #2 10/02/09 33Tcl Conference 2009, Portland Oregon
  • 34.
    Channel Engine Classes •Classes to Make Writing Channels Easy – Based on Andreas Kupries‟s channel API – Both full channels and transformations • Prototype Code on Wiki – http://wiki.tcl.tk/21723 – Introspection to find what features to support 10/02/09 34Tcl Conference 2009, Portland Oregon
  • 35.
    Megawidgets • Make TkWidgets with TclOO Objects – Work in “the same” way – Wrap actual widgets • Prototype Code on Wiki – http://wiki.tcl.tk/21103 • Already Driven Two Features Added – The „variable‟ declaration – Improved method forwarding to object-local commands 10/02/09 35Tcl Conference 2009, Portland Oregon
  • 36.
    Where Next? • TclOOIntended to be Foundation – Fast, light, small, stable, and above all Tcl-ish – It deals with the really complicated bits so you don‟t have to • Features to Add Should be Community- Driven – If you want it, let us know! 10/02/09 36Tcl Conference 2009, Portland Oregon

Editor's Notes

  • #20 Railway stuff reported at EuroTcl 2009
  • #31 Extispicy