TIP 371: Improvements for the dict command

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2017 Conference, Houston/TX, US, Oct 16-20
Send your abstracts to tclconference@googlegroups.com
by Aug 21.
Author:         Thomas Perschak <tombert@gmx.at>
Author:         Trevor Davel <twylite@crypt.co.za>
State:          Draft
Type:           Project
Vote:           Pending
Created:        05-Aug-2010
Post-History:   
Tcl-Version:    8.7

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.

History