Version 6 of Dyso object system

Updated 2004-12-21 07:35:16

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'"
     }
 }

Category Object Orientation [L1 ] [L2 ] [L3 ] [L4 ] [L5 ] [L6 ] [L7 ] [L8 ] [L9 ] [L10 ] [L11 ] [L12 ] [L13 ] [L14 ] [L15 ] [L16 ] [L17 ] [L18 ] [L19 ] [L20 ] [L21 ] [L22 ] [L23 ] [L24 ] [L25 ] [L26 ] [L27 ] [L28 ] [L29 ] [L30 ] [L31 ] [L32 ] [L33 ] [L34 ] [L35 ] [L36 ] [L37 ] [L38 ] [L39 ] [L40 ] [L41 ] [L42 ] [L43 ] [L44 ] [L45 ] [L46 ] [L47 ] [L48 ] [L49 ] [L50 ] [L51 ] [L52 ] [L53 ] [L54 ] [L55 ] [L56 ] [L57 ] [L58 ] [L59 ] [L60 ] [L61 ] [L62 ] [L63 ] [L64 ] [L65 ] [L66 ] [L67 ] [L68 ] [L69 ] [L70 ] [L71 ] [L72 ] [L73 ] [L74 ] [L75 ] [L76 ] [L77 ] [L78 ] [L79 ] [L80 ] [L81 ] [L82 ] [L83 ] [L84 ] [L85 ] [L86 ] [L87 ] [L88 ] [L89 ] [L90 ] [L91 ] [L92 ] [L93 ] [L94 ] [L95 ] [L96 ] [L97 ] [L98 ] [L99 ] [L100 ] [L101 ] [L102 ] [L103 ] [L104 ] [L105 ] [L106 ] [L107 ] [L108 ] [L109 ] [L110 ] [L111 ] [L112 ] [L113 ] [L114 ] [L115 ] [L116 ] [L117 ] [L118 ] [L119 ] [L120 ] [L121 ] [L122 ] [L123 ] [L124 ] [L125 ] [L126 ] [L127 ] [L128 ] [L129 ] [L130 ] [L131 ] [L132 ] [L133 ] [L134 ] [L135 ] [L136 ] [L137 ] [L138 ] [L139 ] [L140 ] [L141 ] [L142 ] [L143 ] [L144 ] [L145 ] [L146 ] [L147 ] [L148 ] [L149 ] [L150 ] [L151 ] [L152 ] [L153 ] [L154 ] [L155 ] [L156 ] [L157 ] [L158 ] [L159 ] [L160 ] [L161 ] [L162 ] [L163 ] [L164 ] [L165 ] [L166 ] [L167 ] [L168 ] [L169 ] [L170 ] [L171 ] [L172 ] [L173 ] [L174 ] [L175 ] [L176 ] [L177 ] [L178 ] [L179 ] [L180 ] [L181 ] [L182 ] [L183 ] [L184 ] [L185 ] [L186 ] [L187 ] [L188 ] [L189 ] [L190 ] [L191 ] [L192 ] [L193 ] [L194 ] [L195 ] [L196 ] [L197 ] [L198 ] [L199 ] [L200 ] [L201 ] [L202 ] [L203 ] [L204 ] [L205 ] [L206 ] [L207 ] [L208 ] [L209 ] [L210 ] [L211 ] [L212 ] [L213 ] [L214 ] [L215 ] [L216 ] [L217 ] [L218 ] [L219 ] [L220 ] [L221 ] [L222 ] [L223 ] [L224 ] [L225 ] [L226 ] [L227 ] [L228 ] [L229 ] [L230 ] [L231 ] [L232 ] [L233 ] [L234 ] [L235 ] [L236 ] [L237 ] [L238 ] [L239 ] [L240 ] [L241 ] [L242 ] [L243 ] [L244 ] [L245 ] [L246 ] [L247 ] [L248 ] [L249 ] [L250 ] [L251 ] [L252 ] [L253 ] [L254 ] [L255 ] [L256 ] [L257 ] [L258 ] [L259 ] [L260 ] [L261 ] [L262 ] [L263 ] [L264 ] [L265 ] [L266 ] [L267 ] [L268 ] [L269 ] [L270 ] [L271 ] [L272 ] [L273 ] [L274 ] [L275 ] [L276 ] [L277 ] [L278 ] [L279 ] [L280 ] [L281 ] [L282 ] [L283 ] [L284 ] [L285 ] [L286 ] [L287 ] [L288 ] [L289 ] [L290 ] [L291 ] [L292 ] [L293 ] [L294 ] [L295 ] [L296 ] [L297 ] [L298 ] [L299 ] [L300 ] [L301 ] [L302 ] [L303 ] [L304 ] [L305 ] [L306 ] [L307 ] [L308 ] [L309 ] [L310 ] [L311 ] [L312 ] [L313 ] [L314 ] [L315 ] [L316 ] [L317 ] [L318 ] [L319 ] [L320 ]