Author: Peter MacDonald <[email protected]>
Author: Robert Seeger <[email protected]>
Author: Donal K. Fellows <[email protected]>
State: Rejected
Type: Project
Vote: Done
Created: 28-Sep-2004
Post-History:
Tcl-Version: 8.7
Tcl-Branch: tip-224
Votes-For: DKF, JN, SL
Votes-Against: DGP, AK
Votes-Present: FV
Rejection-Reason:
Tcl 8.7 already has a new [array default] command from TIP 508. It
does not appear to interact with the proposed [array value] in a
sensible way.
Abstract
The TIP proposes the addition of a new subcommand to the array command: value. The value subcommand would query, reference, and/or initialize an array element with more options for complexity than a simple read.
Rationale
TIP 215 proposed modifying the incr command to auto-initialize
variables which do not exist, rather than throwing an error. After some
debate, it was identified that frequency counting in arrays was the primary
area of interest. In particular, the excessive use of if
/info exists
constructs in such code is inefficient, verbose and rather difficult to read.
Though now we have incr doing automatic initialization, the addition of an
extra subcommand could substantially reduce the complexity of much Tcl code.
Specification
A new subcommand will be added to array, being value.
array value var elem ?value? ?init?
The array value would just return the current contents of the element
elem of the array called var if it exists, or value otherwise. If the
init parameter is provided and resolves to true (as determined by
Tcl_GetBooleanFromObj()
of course), the variable is initialized to value
if it doesn't already exist. The default for value is the empty string, and
the default for init is false.
Reference Implementation
Following is an example Tcl-only implementation of the value subcommand:
proc Array {cmd var elem {amt {}} {init 0}} {
# Implement the Array subcmd value on var(elem):
# - value return the value if defined, else return amt
# initializing if init.
upvar $var uvar
if {[string match $cmd* value]} {
if {[info exists uvar($elem)]} {
return $uvar($elem)
}
if {$init} {
return [set uvar($elem) $amt]
}
return $amt
} else {
error "usage: Array value var elem ?amt? ?init?"
}
}
Discussion
Wangnic notes:
[array value var elem "" false] can be written as [array get var elem]
Array get returns name/value pairs. Array value returns just the value. Also if elem has a
*
in it, there may be multiple matches.Hobbs notes:
TIP 200 already rejected array values
A single item is returned from array value, not a list, as there is no string match on elem.
The problem domain is not list processing (which is expected to be relatively expensive), but rather frequency counting and set matching.
RHSeeger notes:
It would seem more consistant to push for the incr command to include a way to initialize a variable if it doesn't exist (as per TIP 215 or the discussed options contained therein), rather than add an incr subcommand to array.
Being a fan or reintroducing the array values TIP (since the core mailing list indicates it was never fully rejected), I think array value might be a bit confusing, being only off by the lack of plurality. (I was one of the people pushing for TIP 200 though, so take that aspect with a grain of salt)
dkf notes:
The array incr command was removed from this TIP, as its main use case was addressed when TIP 215 was accepted, and the measured consensus was that the proposed extra command offered too little over that. By comparison, array value is more useful and parallels TIP 342 in terms of general high-level capabilities (though there are syntactic differences).
Copyright
This document has been placed in the public domain.