Introduction

... general text, maybe partly from slides/paper ....

Supporting XOTcl 1 in XOTcl 2

In general, the XOTcl 2 environment supports multiple object systems concurrently. Effectively, every object system has different base classes for creating both, objects and classes. Therefore, the object systems can have different different interfaces and names of builtin methods. Currently, XOTcl 2 supports primarily XOTcl 1 and XOTcl 2 (XOTcl 1 provides about twice as many predefined builtin methods).

The support for multiple object systems can be used to load XOTcl 1 and XOTcl 2 scripts into the same interpreter. This makes migration from XOTcl 1 to XOTcl 2 easier. The XOTcl environment provides the predefined command ::xotcl::use to make switching between these objects systems convenient. The following example script shows a single script defining classes of XOTcl 1 and 2:

   package require XOTcl

   namespace eval mypackage {

      # Some definitions for XOTcl 1
      ::xotcl::use xotcl1

      Class C1
      C1 instproc foo {} {puts "hello world"}

      # Some definitions for XOTcl 2
      ::xotcl::use xotcl2

      Class create C2 {
         :method foo {} {puts "hello world"}
      }
   }

"Switching" effectively means the load some libraries if needed and to import either the XOTcl 1 or XOTcl 2 base classes as Object and Class into the current namespace. If the import is not wanted, just use package require to load the necessary pieces.

XOTcl 1 Idioms in XOTcl 2

Defining Objects and Classes

XOTcl 1XOTcl 2
Class ClassName Class create ClassName
Object ObjectName Object create ObjectName
::xotcl::Class ClassName ::xotcl2::Class create ClassName
::xotcl::Object ObjectName ::xotcl2::Object create ObjectName

Defining Methods

XOTcl 1XOTcl 2
Class C
C instproc foo args {...}
C proc bar args {...}
Class create C {
  :method foo args {...}
  :object method bar args {...}
}

Class create C
C method foo args {...}
C object method bar args {...}
Object o
o set x 1
o proc foo args {...}
Object create o {
  set :x 1
  :method foo args {...}
}

Object create o
o eval {set :x 1}
#    ::xotcl::setvar o x 1
o method foo args {...}

Resolvers

XOTcl 2 defines Tcl resolvers for method and variable names to refer to object specific behavior. Withing method bodies these resolver treat names staring with a dot "." specially. If one wants to refer to a name starting with a "." (e.g. the name of a Tcl function starts with a dot), the dot has to be duplicated, or one has to use the usual namespace prefix "::" to refer to a namespaced entity. Note that the XOTcl 2 resolver is used in the XOTcl 1 environment as well.

Invoking Methods

XOTcl 1XOTcl 2
Class C
C instproc foo args {...}
C instproc bar args {
  my foo 1 2 3 ;# invoke own method
  o baz       ;# invoke others method
}
Object o
o proc baz {} {...}
Class create C {
  :method foo args {...}
  :method bar args {
     :foo 1 2 3 ;# invoke own method
     o baz     ;# invoke others method
  }
}
Object create o {
  :method baz {} {...}
}

Accessing Instance Variables from Method Bodies

XOTcl 1XOTcl 2
Class C
C instproc foo args {
  # method scoped variable a
  set a 1
  # instance variable b
  my instvar b
  set b 2
  # global variable/namespaced variable c
  set ::c 3
}
Class create C {
  :method foo args {...}
    # method scoped variable a
    set a 1
    # instance variable b
    set :b 2
    # global variable/namespaced variable c
    set ::c 3
  }
}
my set varname value set :varname value
set newVar [my set otherVar] set newVar [set :otherVar]

set newVar ${:otherVar}
my instvar newVar
set newVar value
set :newVar value
my exists varname info :varname

Accessing Instance Variables of other Objects

XOTcl 1XOTcl 2
obj set varname value obj eval [list set :varname value]
set newVar [obj set otherVar] set newVar [obj eval {set :otherVar}]
obj instvar newVar
set newVar value
::xotcl::importvar obj newVar
set newVar value
obj exists varname obj eval {info exists :varname}

Object Parameters

Method Parameters

Introspection

List methods defined by objects

XOTcl 1XOTcl 2
obj info commands ?pattern? obj info methods ?pattern?
obj info parametercmd ?pattern? obj info methods -methodtype setter ?pattern?
obj info procs ?pattern? obj info methods -methodtype scripted ?pattern?
n.a. obj info methods -methodtype alias ?pattern?
n.a. obj info methods -methodtype forwarder ?pattern?
n.a. obj info methods -methodtype object ?pattern?
n.a. obj info methods -callprotection public|protected ...

List methods defined by classes

cls info instcommands ?pattern? cls info methods ?pattern?
cls info instparametercmd ?pattern? cls info methods -methodtype setter ?pattern?
cls info instprocs ?pattern? cls info methods -methodtype scripted ?pattern?
n.a. cls info methods -methodtype alias ?pattern?
n.a. cls info methods -methodtype forwarder ?pattern?
n.a. cls info methods -methodtype object ?pattern?
n.a. cls info methods -callprotection public|protected ...

List class object specific methods

cls info commands ?pattern? cls object info methods ?pattern?
cls info parametercmd ?pattern? cls object info methods -methodtype setter ?pattern?
cls info procs ?pattern? cls object info methods -methodtype scripted ?pattern?
n.a. cls object info methods -methodtype alias ?pattern?
n.a. cls object info methods -methodtype forwarder ?pattern?
n.a. cls object info methods -methodtype object ?pattern?
n.a. cls object info methods -callprotection public|protected ...

List callable methods

XOTcl 1XOTcl 2
obj info methods ?pattern? obj info callable ?pattern?
n.a. # list only application specific methods
obj info callable -application

List object/class where some method is defined

XOTcl 1XOTcl 2
obj procsearch methodName obj info callable -which methodName

List definition of scripted methods defined by classes

XOTcl 1XOTcl 2
cls info instbody methodName cls info method body methodName
cls info instargs methodName cls info method args methodName
cls info instnonposargs methodName cls info method parameter methodName
cls info instdefault methodName cls info method parameter methodName
cls info instpre methodName cls info method precondition methodName
cls info instpost methodName cls info method postcondition methodName
n.a. cls info method definition methodName

List definition of scripted object specific methods

XOTcl 1XOTcl 2
obj info body methodName obj info method body methodName
obj info args methodName obj info method args methodName
obj info nonposargs methodName obj info method parameter methodName
obj info default methodName obj info method parameter methodName
obj info pre methodName obj info method precondition methodName
obj info post methodName obj info method postcondition methodName
n.a. obj info method definition methodName

For definition of class object specific methods, use modifier object as shown in examples above.

List filter or mixins

XOTcl 1XOTcl 2
obj info filter ?-order? ?-guards? ?pattern? obj info filter ?-order? ?-guards? ?pattern?
cls info filter ?-order? ?-guards? ?pattern? cls object info filter ?-order? ?-guards? ?pattern?
cls info instfilter ?-order? ?-guards? ?pattern? cls info filter ?-order? ?-guards? ?pattern?
obj info mixin ?-order? ?-guards? ?pattern? obj info mixin ?-order? ?-guards? ?pattern?
cls info mixin ?-order? ?-guards? ?pattern? cls object info mixin ?-order? ?-guards? ?pattern?
cls info instmixin ?-order? ?-guards? ?pattern? cls info mixin ?-order? ?-guards? ?pattern?

List definition of methods defined by aliases, setters or forwarders

XOTcl 1XOTcl 2
n.a. obj info method definition methodName
n.a. cls ?object? info method definition methodName

List fully qualified name of method

XOTcl 1XOTcl 2
n.a. obj info method name methodName
n.a. cls ?object? info method name methodName

List type of a method

XOTcl 1XOTcl 2
n.a. obj info method type methodName
n.a. cls ?object? info method type methodName

List the scope of mixin classes

XOTcl 1XOTcl 2
cls info mixinof ?-closure? ?pattern? cls info mixinof -scope object ?-closure? ?pattern?
cls info instmixinof ?-closure? ?pattern? cls info mixinof -scope class ?-closure? ?pattern?
n.a. cls info mixinof -scope all ?-closure? ?pattern?

Check properties of object and classes

XOTcl 1XOTcl 2
obj istype sometype ::xotcl::is obj type sometype

obj info is type sometype
obj ismixin cls ::xotcl::is obj mixin cls

obj info is mixin cls
obj isclass ?cls? ::xotcl::is obj|cls class

obj info is class
obj ismetaclass cls ::xotcl::is obj|cls metaclass
obj info is metaclass
n.a. ::xotcl::is cls baseclass
cls info is baseclass
obj isobject obj2 ::xotcl::is obj|obj2 object
obj info is object

Predefined Methods

Dispatch, Aliases, etc.

Assertions

Method Protection

Incompatibilities

Namespace resolvers

The XOTcl 2 namespace resolvers are use as well when XOTcl 1 is used within XOTcl 2.

Stronger Checking

XOTcl 2 performs stronger checking than XOTcl 1. The requiredness of slots in XOTcl 1 was just a comment, while XOTcl 2 enforces it.

Different results

Exit handlers

The exit hander interface changed from a method of ::xotcl::Object into procs in the ::xotcl namespace. XOTcl 2 provides now:

   ::xotcl::setExitHandler script
   ::xotcl::getExitHandler
   ::xotcl::unsetExitHandler

Last modified: Tue Feb 23 13:41:35 CET 2010
XOTcl 1XOTcl 2
obj check checkptions ::xotcl::assertion obj check checkptions
obj info check ::xotcl::assertion obj check
obj invar conditions ::xotcl::assertion obj object-invar conditions
obj info invar ::xotcl::assertion obj object-invar
cls instinvar conditions ::xotcl::assertion cls class-invar conditions
cls info instinvar ::xotcl::assertion cls class-invar
cls invar conditions ::xotcl::assertion cls object-invar conditions
cls info invar ::xotcl::assertion cls object-invar