Check-in [d894c3b20e]

Login
FlightAware bounty program for improvements to Tcl and certain Tcl packages.

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

Overview
Comment:Tip 508: Clarify behavior of default value with read traces
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:d894c3b20e7878e39cdf3934fe693a17185e1937a5a2bf66d20d681eecd0b2e1
User & Date: fbonnet 2018-06-09 21:04:59
Context
2018-06-10
20:44
Formatting tweaking check-in: 11ef41277a user: dkf tags: trunk
2018-06-09
21:04
Tip 508: Clarify behavior of default value with read traces check-in: d894c3b20e user: fbonnet tags: trunk
2018-06-06
19:46
Changed e-mail check-in: 9da553226b user: rene tags: trunk
Changes

Changes to tip/508.md.

54
55
56
57
58
59
60





61
62
63
64
65
66
67
...
157
158
159
160
161
162
163
164
























165
166
167
168
169
170
171
172
173
174
  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
................................................................................
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.

# Copyright

This document has been placed in the public domain.







>
>
>
>
>







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>










54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  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.

The default value is only used as a last resort where an error would normally
occur when accessing a non-existing array element. Read traces are processed
before that point and are free to set the element value or modify the default
value.

# Examples

Initial state:

	% array exists var
	0
................................................................................
variable:

	% array exists var2
	0
	% array default set var2 1234
	% array exists var2
	1

The default value can be set, modified or unset by trace handlers:

	% set var(foo)
	can't read "var(foo)": no such element in array
	% trace add variable var read setRandomDefault
	% proc setRandomDefault {name1 name2 op} {
	    upvar $name1 arr
	    array default set arr [expr {rand()}]
	}
	% set var(foo)
	0.9132881643778124
	% set var(foo)
	0.6341786978925479
	% set var(foo)
	0.5976932461362766 
	% array default get var
	0.5976932461362766
	% proc setRandomDefault {name1 name2 op} {
	    upvar $name1 arr
	    array default unset arr
	}
	% set var(foo)
	can't read "var(foo)": no such element in array


# Implementation

The proposed implementation is available on branch
[tip-508](https://core.tcl.tk/tcl/timeline?r=tip-508) in the Tcl Fossil
repository.

# Copyright

This document has been placed in the public domain.