Check-in [d894c3b20e]

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: 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
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to tip/508.md.

    54     54     existing scalar variable or an invalid array name.
    55     55   * <code>array default exists <em>arrayName</em></code>: Test whether
    56     56     _arrayName_ has a default value. Return false if _arrayName_ does not exist.
    57     57     Raise an error if _arrayName_ is not an array variable.
    58     58   * <code>array default unset <em>arrayName</em></code>: Unset _arrayName_ default
    59     59     value. Does nothing if _arrayName_ does not exist. Raise an error if
    60     60     _arrayName_ is not an array variable.
           61  +
           62  +The default value is only used as a last resort where an error would normally
           63  +occur when accessing a non-existing array element. Read traces are processed
           64  +before that point and are free to set the element value or modify the default
           65  +value.
    61     66   
    62     67   # Examples
    63     68   
    64     69   Initial state:
    65     70   
    66     71   	% array exists var
    67     72   	0
................................................................................
   157    162   variable:
   158    163   
   159    164   	% array exists var2
   160    165   	0
   161    166   	% array default set var2 1234
   162    167   	% array exists var2
   163    168   	1
   164         -		
          169  +
          170  +The default value can be set, modified or unset by trace handlers:
          171  +
          172  +	% set var(foo)
          173  +	can't read "var(foo)": no such element in array
          174  +	% trace add variable var read setRandomDefault
          175  +	% proc setRandomDefault {name1 name2 op} {
          176  +	    upvar $name1 arr
          177  +	    array default set arr [expr {rand()}]
          178  +	}
          179  +	% set var(foo)
          180  +	0.9132881643778124
          181  +	% set var(foo)
          182  +	0.6341786978925479
          183  +	% set var(foo)
          184  +	0.5976932461362766 
          185  +	% array default get var
          186  +	0.5976932461362766
          187  +	% proc setRandomDefault {name1 name2 op} {
          188  +	    upvar $name1 arr
          189  +	    array default unset arr
          190  +	}
          191  +	% set var(foo)
          192  +	can't read "var(foo)": no such element in array
          193  +
   165    194   
   166    195   # Implementation
   167    196   
   168    197   The proposed implementation is available on branch
   169    198   [tip-508](https://core.tcl.tk/tcl/timeline?r=tip-508) in the Tcl Fossil
   170    199   repository.
   171    200   
   172    201   # Copyright
   173    202   
   174    203   This document has been placed in the public domain.