info class and info object
- NAME
- info class, info object — introspection for classes and objects
- SYNOPSIS
- DESCRIPTION
- CLASS INTROSPECTION
- info class call class method
- info class constructor class
- info class definition class method
- info class destructor class
- info class filters class
- info class forward class method
- info class instances class ?pattern?
- info class methods class ?options...?
- info class methodtype class method
- info class mixins class
- info class subclasses class ?pattern?
- info class superclasses class
- info class variables class
- info class constructor class
- OBJECT INTROSPECTION
- info object call object method
- info object class object ?className?
- info object definition object method
- info object filters object
- info object forward object method
- info object isa category object ?arg?
- info object isa class object
- info object isa metaclass object
- info object isa mixin object class
- info object isa object object
- info object isa typeof object class
- info object isa metaclass object
- info object methods object ?option...?
- info object methodtype object method
- info object mixins object
- info object namespace object
- info object variables object
- info object vars object ?pattern?
- info object class object ?className?
- EXAMPLES
- SEE ALSO
- KEYWORDS
NAME
info class, info object — introspection for classes and objectsSYNOPSIS
package require TclOOinfo object subcommand object ?arg ...?
info class subcommand class ?arg ...? info option ?arg arg ...?
DESCRIPTION
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.CLASS INTROSPECTION
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
“unknown”
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 definition 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 definition, 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 definition 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 definition, 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 class (i.e. that are automatically present in the class's methods, constructor and destructor).
OBJECT INTROSPECTION
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
“unknown”
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 definition 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 definition, 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. Note that this is different from the list returned by info object variables; that can include variables that are currently unset, whereas this can include variables that are not automatically included by any of object's methods (or those of its class, superclasses or mixins).
EXAMPLES
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 looking up the definition; it is implemented 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 (in such cases, using info object call as above is the simplest way of doing this by far):
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] }