Check-in [ba804fbdae]

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to [email protected]
or submit via the online form by Sep 9.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:TIP #508: Initial draft complete
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ba804fbdae3e3037709128d99c7c0b67a10db440a69c74926be13fa2759bf8eb
User & Date: fbonnet 2018-05-14 22:04:54
Context
2018-05-17
12:58
TIP #491 Done check-in: a23088053b user: jan.nijtmans tags: trunk
2018-05-14
22:04
TIP #508: Initial draft complete check-in: ba804fbdae user: fbonnet tags: trunk
2018-05-13
22:27
TIP #508: Added link to the implementation (branch tip-508) check-in: 8d21f71ce2 user: fbonnet tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to tip/508.md.

    13     13   
    14     14   This TIP proposes a new `[array default]` subcommand that allows default values
    15     15   for arrays, causing that value to be returned any time an attempt is made to
    16     16   access an element of the array that isn't present.
    17     17   
    18     18   # Rationale
    19     19   
    20         -TODO
           20  +For its most basic usages, a Tcl array variable can be seen as a collection of
           21  +scalar variables. However, an array also has its own existence as a variable and
           22  +provides specficic actions on itself and on its elements. For exampe, the
           23  +regular way to check for a variable existence is to use the `[info exists]`
           24  +command, and this is true for both arrays and scalars (including array
           25  +elements); likewise, the `[set]` command is used to read and write the value of
           26  +a scalar vars and array elements, however array variables have no value in
           27  +themselves but provide a way to get and set several of its elements at once.
           28  +
           29  +For more advanced usages, one must get past the simple 'collection of scalars'
           30  +viewpoint. A typical example is sparse arrays: at present, an implementation
           31  +requires a combination of `[array exists]` (or `[info exists]`) and `[set]`: the
           32  +former to test the presence of a key in an array, the latter to get or set the
           33  +actual value, and in this order. Failure to do so is likely to cause runtime
           34  +errors.
           35  +
           36  +This document therefore proposes to add a new `[array default]` subcommand in
           37  +order to support such advanced usages. Sparse arrays built on this new feature
           38  +will be simplerw, more robust, faster, all the while being memory efficient.
    21     39   
    22     40   # Specification
    23     41   
    24         -TODO
           42  +A new `array default` subcommand will be created with the following syntax:
           43  +
           44  +> `array default ` _`option arrayName ?arg?`_ : Default value management. 
           45  +   
           46  +The following _option_ values are supported:
           47  +
           48  +* <code>array default get <em>arrayName</em></code>: Get _arrayName_
           49  +  default value. Raise an error if _arrayName_ is not an array variable or if it
           50  +  has no default value.
           51  +* <code>array default set <em>arrayName</em> <em>value</em></code>: Set
           52  +  _arrayName_ default value. If the variable _arrayName_ does not already exist,
           53  +  it is created with an empty array value. Raise an error if _arrayName_ is an
           54  +  existing scalar variable or an invalid array name.
           55  +* <code>array default exists <em>arrayName</em></code>: Test whether
           56  +  _arrayName_ has a default value. Return false if _arrayName_ does not exist.
           57  +  Raise an error if _arrayName_ is not an array variable.
           58  +* <code>array default unset <em>arrayName</em></code>: Unset _arrayName_ default
           59  +  value. Does nothing if _arrayName_ does not exist. Raise an error if
           60  +  _arrayName_ is not an array variable.
           61  +
           62  +# Examples
           63  +
           64  +Initial state:
           65  +
           66  +	% array exists var
           67  +	0
           68  +	% array default exists var
           69  +	0
           70  +	% array default get var
           71  +	"var" isn't an array
           72  +	% set var(a)
           73  +	can't read "var(a)": no such variable
           74  +
           75  +Create array with one entry:
           76  +
           77  +	% set var(a) 1
           78  +	1
           79  +	% array exists var
           80  +	1
           81  +	% array default exists var
           82  +	0
           83  +
           84  +Attempt to read missing entry:
           85  +
           86  +	% set var(b)
           87  +	can't read "var(b)": no such element in array
           88  +
           89  +Add default value:
           90  +
           91  +	% array default set var 0
           92  +	% array default exists var
           93  +	1
           94  +	% array default get var
           95  +	0
           96  +
           97  +Default value is only used by missing entries:
           98  +
           99  +	% info exists var(a)
          100  +	1
          101  +	% set var(a)
          102  +	1
          103  +	% info exists var(b)
          104  +	0
          105  +	% set var(b)
          106  +	0
          107  +
          108  +You can `lappend` to a missing entry and it will behave as expected:
          109  +
          110  +	% lappend var(b) 2
          111  +	0 2
          112  +	% info exists var(b)
          113  +	1
          114  +
          115  +Changing the default value only impact missing entries:
          116  +
          117  +	% info exists var(c)
          118  +	0
          119  +	% set var(c)
          120  +	0
          121  +	% array default set var 9999
          122  +	% array default get var
          123  +	9999
          124  +	% set var(a)
          125  +	1
          126  +	% set var(b)
          127  +	0 2
          128  +	% set var(c)
          129  +	9999
          130  +
          131  +Default values don't show in entry lists or iterations:
          132  +
          133  +	% array names var
          134  +	a b
          135  +	% array get var
          136  +	a 1 b {0 2}
          137  +	% array for {key value} var {puts "$key => $value"}
          138  +	a => 1
          139  +	b => 0 2
          140  +	% parray var
          141  +	var(a) = 1
          142  +	var(b) = 0 2	
          143  +
          144  +Unsetting the default value will only invalidate missing entries:
          145  +
          146  +	% array default unset var
          147  +	% set var(a)
          148  +	1
          149  +	% set var(b)
          150  +	0 2
          151  +	% info exists var(c)
          152  +	0
          153  +	% set var(c)
          154  +	can't read "var(c)": no such element in array
          155  +
          156  +Setting the default value on a non-existing variable implicitly creates an array
          157  +variable:
    25    158   
    26         -	array default get arrayName
    27         -	array default set arrayName value
    28         -	array default exists arrayName
    29         -	array default unset arrayName
          159  +	% array exists var2
          160  +	0
          161  +	% array default set var2 1234
          162  +	% array exists var2
          163  +	1
          164  +		
    30    165   
    31    166   # Implementation
    32    167   
    33    168   The proposed implementation is available on branch
    34    169   [tip-508](https://core.tcl.tk/tcl/timeline?r=tip-508) in the Tcl Fossil
    35    170   repository.
    36    171   
    37    172   # Copyright
    38    173   
    39    174   This document has been placed in the public domain.