TIP 342: Dict Get With Default

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:		Lars Hellström <Lars.Hellstrom@residenset.net>
State:		Draft
Type:		Project
Vote:		Pending
Tcl-Version:	8.7
Created:	27-Nov-2008
Keywords:	dictionary, default value
Post-History:	

Abstract

A new subcommand of dict is proposed, which returns a dictionary value if it exists and returns a per-call default otherwise.

Specification

The dict command will get a new subcommand

dict getwithdefault dictionary key ?key ...? value

(I consider the name of this subcommand very much open for discussion) which modulo error messages behaves like

 proc dict_getwithdefault {D args} {
     if {[dict exists $D {*}[lrange $args 0 end-1]]} then {
         dict get $D {*}[lrange $args 0 end-1]
     } else {
         lindex $args end
     }
 }

i.e., it returns the value from the dictionary corresponding to the sequence of keys if it exists, or the default value otherwise. As with dict exists, it is OK (and will cause the default value to be returned) if one of the keys is missing from its dictionary, but an error is thrown if this path of keys cannot be traversed because the value associated with the previous key is not a dictionary.

Rationale

It is clear that getting a value from a dictionary if it exists and using a default otherwise is a common operation, but it is also clear that this can be carried out with a combination of existing Tcl commands. Hence the issue is whether a new subcommand for this improves efficiency and convenience of this operation enough to justify the possible bloat it brings.

Alternative Methods

One approach that has been suggested for providing default values is to combine dict get with dict merge, like so:

  dict get [dict merge {-apa bar} $D] -apa

This approach is however appropriate mainly in situations where several keys are given fixed defaults simultaneously. Compared to dict getwithdefault, it has the following disadvantages:

Hence although dict merge is sometimes appropriate for providing defaults, it is not a universal solution.

The basic approach is instead to, as in the dict_getwithdefault proc above, first use dict exists and then dict get if the value existed. Problems with this approach are:

The only way to avoid the redundance of an extra look-up seems to be to combine dict get with catch, like so:

  if {[catch {dict get $D -apa} value]} then {set value bar} else {set value}

but this has the disadvantage of hiding other sources of error, such as D not being a dictionary in the first place. This kind of error in a normal processing path is also considered poor style by some.

Implementation Choices

Even if it is deemed appropriate to have a dedicated subcommand of dict for this, it could be argued that it needn't be part of the compiled Tcl core; since dict is an ensemble, anyone can extend it at the script level and "the core can do without this bloat". However, it turns out than an in-core implementation is very easy whereas the alternatives are not so easy.

Concretely, the necessary DictGetWithDefaultCmd is a trivial modification of DictExistsCmd, to take one extra argument after the keys and change the final

  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(valuePtr != NULL));

to

  Tcl_SetObjResult(interp, valuePtr != NULL ? valuePtr : objv[objc-1]);

It is nowhere near as easy to do this in a well-behaved extension, since DictExistsCmd relies on TclTraceDictPath to do most of the work, and the latter is AFAICT at best available in the internal stubs table.

A script-level implementation is certainly possible, but the minute details of producing core-looking error messages in this case appears considerable both compared to the functional parts of the command and compared to the amount of code needed to do it in the core.

Reference Implementation

An implementation is provided on SF, in patch #2370575. https://sourceforge.net/support/tracker.php?aid=2370575

Copyright

This document has been placed in the public domain.

History