TIP 501: string is dict

Login
Author:         Sean Woods <[email protected]>
State:          Final
Type:           Project
Vote:           Done
Created:        12-Feb-2018
Post-History:
Keywords:       Tcl,string
Tcl-Version:    8.7
Tcl-Branch:     tip-501
Vote-Summary:   Accepted 5/0/1
Votes-For:      DKF, KBK, JN, FV, SL
Votes-Against:  none
Votes-Present:  BG

Abstract

This TIP proposes the addition of an is dict test to the string ensemble. The command will return true if the value is a valid dict, and false otherwise.

Rationale

Currently the means to test of a string is a leaf node is to check if the string is a list and it's length is divisible by 2. The problem is that this requires 2 different calls from the interpreter, and generates a dict to list conversion for the most common case (namely that the value is, indeed, a dict.)

This TIP proposes a shortcut in C to check to see if the internal representation is already a dict, and provide a shortcut for the most common case.

Implementation

A new branch has been added to the tcl fossil system tip-501

The crux of the implementation is adding "dict" to the IS ensemble:

case STR_IS_DICT:
/*
 * We ignore the strictness here, since empty strings are always
 * well-formed lists.
 */
int dresult, dsize;
dresult = Tcl_DictObjSize(NULL, objPtr, &dsize);
result = (dresult==TCL_OK) ? 1 : 0;
break;