See the file man.macros.
info class, info object - introspection for classes and objects
package require TclOO
info object subcommand object ?arg ...
info class subcommand class ?arg ...
The commands
info object and
info class are ensemble commands that
provide introspection capabilities to the object system, with the
subcommand argument designating which aspect is to be inspectected and
the
object or
class argument naming the object or class to be
inspected.
The following
subcommand values are supported by
info object:
- info object call object method
- Returns a description of the method implementations that are used to
provide object's implementation of method. This consists of
a list of lists of four elements, where each sublist consists of a word
that describes the general type of method implementation (being one of
method for an ordinary method, filter for an applied filter,
and unknown for a method that is invoked as part of unknown method
handling), a word giving the name of the particular method invoked (which
is always the same as method for the method type, and for
the unknown type), a word giving what defined the method (the fully
qualified name of the class, or the literal string object if the
method implementation is on an instance), and a word describing the type
of method implementation (see info object methodtype).
Note that there is no inspection of whether the method implementations actually
use
next to transfer control along the call chain.
- info object class object ?className?
- If className is unspecified, this subcommand returns class of the
object object. If className is present, this subcommand
returns a boolean value indicating whether the object is of that
class.
- info object definition object method
- This subcommand returns a description of the definition of the method
named method of object object. The defintion is described as
a two element list; the first element is the list of arguments to the
method in a form suitable for passing to another call to proc or a
method defintion, and the second element is the body of the method.
- info object filters object
- This subcommand returns the list of filter methods set on the object.
- info object forward object method
- This subcommand returns the argument list for the method forwarding called
method that is set on the object called object.
- info object isa category object ?arg?
- This subcommand tests whether an object belongs to a particular category,
returning a boolean value that indicates whether the object
argument meets the criteria for the category. The supported categories
are:
- info object isa class object
- This returns whether object is a class (i.e. an instance of
oo::class or one of its subclasses).
- info object isa metaclass object
- This returns whether object is a class that can manufacture classes
(i.e. is oo::class or a subclass of it).
- info object isa mixin object class
- This returns whether class is directly mixed into
object.
- info object isa object object
- This returns whether object really is an object.
- info object isa typeof object class
- This returns whether class is the type of object (i.e.
whether object is an instance of class or one of its
subclasses, whether direct or indirect).
- info object methods object ?option...?
- This subcommand returns a list of all public (i.e. exported) methods of
the object called object. Any of the following options may
be specified, controlling exactly which method names are returned:
- -all
- If the -all flag is given, the list of methods will include those
methods defined not just by the object, but also by the object's class and
mixins, plus the superclasses of those classes.
- -private
- If the -private flag is given, the list of methods will also
include the private (i.e. non-exported) methods of the object (and
classes, if -all is also given).
- info object methodtype object method
- This subcommand returns a description of the type of implementation used
for the method named method of object object. When the
result is method, further information can be discovered with
info object definition, and when the result is
forward, further information can be discovered with info object
forward.
- info object mixins object
- This subcommand returns a list of all classes that have been mixed into
the object named object.
- info object namespace object
- This subcommand returns the name of the internal namespace of the object
named object.
- info object variables object
- This subcommand returns a list of all variables that have been declared
for the object named object (i.e. that are automatically present in
the object's methods).
- info object vars object ?pattern?
- This subcommand returns a list of all variables in the private namespace
of the object named object. If the optional pattern argument
is given, it is a filter (in the syntax of a string match glob
pattern) that constrains the list of variables returned.
The following
subcommand values are supported by
info class:
- info class call class method
- Returns a description of the method implementations that are used to
provide a stereotypical instance of class's implementation of
method (stereotypical instances being objects instantiated by a
class without having any object-specific definitions added). This consists
of a list of lists of four elements, where each sublist consists of a word
that describes the general type of method implementation (being one of
method for an ordinary method, filter for an applied filter,
and unknown for a method that is invoked as part of unknown method
handling), a word giving the name of the particular method invoked (which
is always the same as method for the method type, and for
the unknown type), a word giving the fully qualified name of the
class that defined the method, and a word describing the type of method
implementation (see info class methodtype).
Note that there is no inspection of whether the method implementations actually
use
next to transfer control along the call chain.
- info class constructor class
- This subcommand returns a description of the definition of the constructor
of class class. The defintion is described as a two element list;
the first element is the list of arguments to the constructor in a form
suitable for passing to another call to proc or a method defintion,
and the second element is the body of the constructor. If no constructor
is present, this returns the empty list.
- info class definition class method
- This subcommand returns a description of the definition of the method
named method of class class. The defintion is described as a
two element list; the first element is the list of arguments to the method
in a form suitable for passing to another call to proc or a method
defintion, and the second element is the body of the method.
- info class destructor class
- This subcommand returns the body of the destructor of class class.
If no destructor is present, this returns the empty string.
- info class filters class
- This subcommand returns the list of filter methods set on the class.
- info class forward class method
- This subcommand returns the argument list for the method forwarding called
method that is set on the class called class.
- info class instances class ?pattern?
- This subcommand returns a list of instances of class class. If the
optional pattern argument is present, it constrains the list of
returned instances to those that match it according to the rules of
string match.
- info class methods class ?options...?
- This subcommand returns a list of all public (i.e. exported) methods of
the class called class. Any of the following options may be
specified, controlling exactly which method names are returned:
- -all
- If the -all flag is given, the list of methods will include those
methods defined not just by the class, but also by the class's
superclasses and mixins.
- -private
- If the -private flag is given, the list of methods will also
include the private (i.e. non-exported) methods of the class (and
superclasses and mixins, if -all is also given).
- info class methodtype class method
- This subcommand returns a description of the type of implementation used
for the method named method of class class. When the result
is method, further information can be discovered with info
class definition, and when the result is forward,
further information can be discovered with info class forward.
- info class mixins class
- This subcommand returns a list of all classes that have been mixed into
the class named class.
- info class subclasses class ?pattern?
- This subcommand returns a list of direct subclasses of class class.
If the optional pattern argument is present, it constrains the list
of returned classes to those that match it according to the rules of
string match.
- info class superclasses class
- This subcommand returns a list of direct superclasses of class
class in inheritance precedence order.
- info class variables class
- This subcommand returns a list of all variables that have been declared
for the class named Iclass (i.e. that are automatically present in the
class's methods, constructor and destructor).
Note that these commands are likely to be renamed in the future.
Every object necessarily knows what its class is; this information is trivially
extractable through introspection:
oo::class create c c create o puts [
info object class o]
→ prints "::c" puts [
info object class c]
→ prints "::oo::class"
The introspection capabilities can be used to discover what class implements a
method and get how it is defined. This procedure illustrates how:
proc getDef {obj method} {
foreach inf [
info object call $obj $method] {
lassign $inf calltype name locus methodtype
# Assume no forwards or filters, and hence no $calltype
# or $methodtype checks...
if {$locus eq "object"} {
return [
info object definition $obj $name]
} else {
return [
info class definition $locus $name]
}
}
error "no definition for $method" }
This is an alternate way of implementing the definition lookup is by manually
scanning the list of methods up the inheritance tree. This code assumes that
only single inheritance is in use, and that there is no complex use of
mixed-in classes:
proc getDef {obj method} {
if {$method in [
info object methods $obj]} {
# Assume no forwards
return [
info object definition $obj $method]
}
set cls [
info object class $obj]
while {$method ni [
info class methods $cls]} {
# Assume the simple case
set cls [lindex [
info class superclass $cls] 0]
if {$cls eq {}} {
error "no definition for $method"
}
}
# Assume no forwards
return [
info class definition $cls $method] }
oo::class(n), oo::define(n), oo::object(n), self(n)
introspection, object