TIP #117 Version 1.4: Object Type Introspection

This is not necessarily the current version of this TIP.


TIP:117
Title:Object Type Introspection
Version:$Revision: 1.4 $
Author:Peter Spjuth <peter dot spjuth at space dot se>
State:Draft
Type:Project
Tcl-Version:8.5
Vote:Pending
Created:Friday, 01 November 2002

Abstract

This TIP proposes to add a subcommand to info 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.

This small gap in Tcl's introspection abilities can be filled with a simple addition to the info command.

Specification

A new subcommand, objtype, is added to info with the syntax:

info 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:

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

Reference Implementation

static int
InfoObjTypeCmd(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.


Powered by TclThis is not necessarily the current version of this TIP.

TIP AutoGenerator - written by Donal K. Fellows