TIP #117 Version 1.1: Object Type Introspection

This is not necessarily the current version of this TIP.


TIP:117
Title:Object Type Introspection
Version:$Revision: 1.1 $
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.

Since the command is mainly for debugging it gives a human readable output, but guarantees that it is also a list to simplify script handling of the result.

Specification

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

info objtype value

It returns a list where the second element is the name of value's object type, or "none" if there is no internal representation. The fourth element is "yes" or "no" depending on if the object has a valid string representation or not. The first and third elements are static strings "Type:" and "StringRep:" to make it human readable.

Examples:

% set apa hejsan
hejsan
% info objtype $apa
Type: none StringRep: yes
% string length $apa
6
% info objtype $apa
Type: string StringRep: yes
% regexp $apa hoppsan
0
% info objtype $apa
Type: regexp StringRep: yes
% info objtype [expr 1+1]
Type: int StringRep: no

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 != 3) {
        Tcl_WrongNumArgs(interp, 2, objv, "value");
        return TCL_ERROR;
    }

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

    Tcl_ListObjAppendElement(interp, listPtr,
            Tcl_NewStringObj("Type:", -1));
    Tcl_ListObjAppendElement(interp, listPtr, Tcl_NewStringObj(
            (objv[2]->typePtr != NULL) ? objv[2]->typePtr->name : "none", -1));
    Tcl_ListObjAppendElement(interp, listPtr,
            Tcl_NewStringObj("StringRep:", -1));
    Tcl_ListObjAppendElement(interp, listPtr,
            Tcl_NewStringObj((objv[2]->bytes != NULL) ? "yes" : "no", -1));

    Tcl_SetObjResult(interp, listPtr);
    return TCL_OK;
}

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