TIP #371: Improvements for the dict command


TIP:371
Title:Improvements for the dict command
Version:$Revision: 1.3 $
Authors: Thomas Perschak <tombert at gmx dot at>
Trevor Davel <twylite at crypt dot co dot za>
State:Draft
Type:Project
Tcl-Version:8.7
Vote:Pending
Created:Thursday, 05 August 2010

Abstract

The dict command is limited by allowing manipulation of only the first level of key elements. Not only should the dict get accept a nested key list, but also the other commands like dict replace.

Specification And Example

The following line:

dict get {-range {-values {a b c} -base M} -name myname} \
        -range -base

Results in:

M

But how to change the base? I suggest the following dict syntax extension:

dict replace {-range {-values {a b c} -base M} -name myname} \
        {-range -base} k

Results in:

-range {-values {a b c} -base k} -name myname

Allowing a nested key list would not break any previous code, but substantially improve the dict command.

Rationale

The dict command is the basis for handling database like structures. By allowing nested keys this would give more freedom in organizing these structures like the example above.


Comments

Twylite 2010/08/17: The specification states that "Allowing a nested key list would not break any previous code". This is not correct, for example:

dict replace {"Jane Smith" "11 Foo Road" "John Doe" "Address unknown"} {Jane Smith} new_address
-> {Jane Smith} new_address {John Doe} {Address unknown}

Existing code that uses dict replace in conjunction with keys that are valid lists of 2 or more elements would break.

Also, drawing from experience, the nested key approach is a source of subtle bugs. It is natural to write code such as:

dict replace $dict $key $value

but that contains a bug that is often missed during testing. The correct approach would be:

dict replace $dict [list $key] $value

Copyright

This document has been placed in the public domain.


Powered by Tcl[Index] [History] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows