MReconstructable
toe mixin MReconstructable {
  public:
  method -novars reset {args} {toe reset [self object] {*}$args}
  method -novars copy {args} {toe copy [self object] 0 {*}$args}
  method -novars copyadopting {args} {toe copy [self object] 1 {*}$args}
  method -novars clone {args} {toe clone [self object] {*}$args}
}

MReconstructable mixes in runtime support for resetting an object to its initial state, and for constructing a fresh copy of the object in three different ways:

  1. Copy the object and each object in its inheritance hierarchy, without changing any ownership
  2. Copy the object and each object in its inheritance hierarchy, with the new copies adopting objects, if any, owned by the copies' models.
  3. Copy the object, its object hierarchy, and any other objects owned throughout the hierarchy, recursively; update values of class variables containing references to owned objects, to be current for the new objects.

MReconstructable only needs to be mixed in to the class for the immediate object to be copied. Input arguments may be optionally supplied for all object resetting and copying methods; these arguments are provided to the constructor of the immediate object only.



TDeque/TStack/TQueue
toe interface IDeque {
  method push item
  method poke item
  method pop
  method pull
  method size
}

toe interface IStack {
  method push item
  method pop {}
  method size {}
  method peek {}
}

toe interface IQueue {
  method push item
  method pull {}
  method size {}
  method peek {}
}

toe class TAbstractDeque abstracts IDeque {
  protected variable List {}
}

toe class TDeque inherits TAbstractDeque implements IDeque {
  public:
  method push {item} {set List [linsert $List 0 "$item"];return}
  method poke {item} {lappend List "$item";return}
  method pull {} {K [lindex $List end] [set List [lrange $List 0 end-1]]}
  method pop {} {set List [lassign $List item];return $item}
  method size {} {return [llength $List]}
  private method K {x y} {set x}
}

toe class TStack inherits TAbstractDeque implements IStack {
  public:
  method push {item} {set List [linsert $List 0 "$item"];return}
  method pop {} {set List [lassign $List item];return $item}
  method size {} {return [llength $List]}
  method peek {} {return [lindex $List 0]}
}

toe class TQueue inherits TAbstractDeque implements IQueue {
  public:
  method push {item} {set List [linsert $List 0 "$item"];return}
  method pull {} {K [lindex $List end] [set List [lrange $List 0 end-1]]}
  method size {} {return [llength $List]}
  method peek {} {return [lindex $List end]}
  private method K {x y} {set x}
}

TDeque, TStack and TQueue demonstrate the use of interfaces, and how a class can abstract or implement an interface to present a clear design. TAbstractDeque provides both storage and an abstract interface. TDeque provides a full implementation of the interface, while TStack and TQueue provide partial implementations of the abstract interface.