TIP 117: Object Type Introspection

Login
Author:         Peter Spjuth <[email protected]>
State:          Withdrawn
Type:           Project
Vote:           Pending
Created:        01-Nov-2002
Post-History:   
Tcl-Version:    8.5
Obsoleted-By:	214

Abstract

This TIP proposes to add a command to give information on the current internal representation of an object.

Rationale

When trying to understand the internals of objects and when trying to track down shimmering problems it is very helpful to be able to see what type an object currently has.

Specification

A new package called "Debug" is created as part of the core.

A new command, objtype, is added to the Debug package. The command has the syntax:

Debug::objtype ?value?

If no value is submitted, a list of all currently registered object types are returned.

If a value is submitted, it returns a list where the first element is the name of value's object type, or "none" if there is no internal representation. The second element is a boolean stating if the object has a valid string representation.

Examples:

% Debug::objtype
boolean index double end-offset wideInt list cmdName bytecode
nsName procbody bytearray int {array search} string
% set apa hejsan
hejsan
% Debug::objtype $apa
none 1
% string length $apa
6
% Debug::objtype $apa
string 1
% regexp $apa hoppsan
0
% Debug::objtype $apa
regexp 1
% Debug::objtype [expr 1+1]
int 0

Discussion

The first proposal was to add this as a subcommand to info. This was considered bad since this should be a pure debug command and should never be used in live code. Thus the proposition came up to create a Debug package and put it in there to emphasise its debug nature. The Debug package can also be extended in the future with other utilities.

Reference Implementation

static int
ObjTypeCmd(dummy, interp, objc, objv)
    ClientData dummy;		/* Not used. */
    Tcl_Interp *interp;	/* Current interpreter. */
    int objc;			/* Number of arguments. */
    Tcl_Obj *CONST objv[];	/* Argument objects. */
{
    Tcl_Obj *listPtr;

    if ((objc != 2) && (objc != 3)) {
        Tcl_WrongNumArgs(interp, 2, objv, "?value?");
        return TCL_ERROR;
    }

    listPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);

    if (objc == 2) {
        Tcl_AppendAllObjTypes(interp, listPtr);
    } else {
        Tcl_ListObjAppendElement(interp, listPtr, Tcl_NewStringObj(
                (objv[2]->typePtr != NULL) ? objv[2]->typePtr->name : "none", -1));
        Tcl_ListObjAppendElement(interp, listPtr,
                Tcl_NewBooleanObj(objv[2]->bytes != NULL));
    }
    Tcl_SetObjResult(interp, listPtr);
    return TCL_OK;
}

Future possibilities

For further object introspection the command can easily be extended by options.

Copyright

This document has been placed in the public domain.