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