Check-in [11ef41277a]

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

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

Overview
Comment:Formatting tweaking
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:11ef41277a212ed75bc03a21e29d523752a5717e7d81e70fa8b1a47c0c5be704
User & Date: dkf 2018-06-10 20:44:07
Context
2018-06-10
20:49
Fix some spelling and awkward wording check-in: 52d37fea35 user: dkf tags: trunk
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
Changes

Changes to tip/508.md.

7
8
9
10
11
12
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
...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	Post-History:   
	Keywords:       Tcl,array
	Tcl-Version:	8.7
-----

# Abstract

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.

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
................................................................................
	% 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







|








|

|



|

|




|

|



|

|
|


|


|
|


|

|
|
>
|




|







 







|







7
8
9
10
11
12
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
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
	Post-History:   
	Keywords:       Tcl,array
	Tcl-Version:	8.7
-----

# Abstract

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 simpler, 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_?

The following _option_ values are supported:

* **array default get** _arrayName_: Get _arrayName_
  default value. Raise an error if _arrayName_ is not an array variable or if it
  has no default value.
* **array default set** _arrayName value_: Set the default value for
  the array _arrayName_. 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.
* **array default exists** _arrayName_: Test whether array
  _arrayName_ has a default value. Return false if _arrayName_ does not exist.
  Raise an error if _arrayName_ exists but is not an array variable.
* **array default unset** _arrayName_: Unsets _arrayName_'s default
  value so that the array no longer has a default. 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
................................................................................
	% set var(a)
	1
	% set var(b)
	0 2
	% set var(c)
	9999

Default values do not 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