Tcl Source Code

Hex Artifact Content
Login

Artifact 11b07222618420c3e7b7d434083d194981fbb654:

Attachment "dict-getwithdefault.tip" to ticket [2370575fff] added by lars_h 2008-12-02 20:40:11.
0000: 54 49 50 3a 20 20 20 20 20 20 20 20 20 20 20 20  TIP:            
0010: 0d 54 69 74 6c 65 3a 20 20 20 20 20 20 20 20 20  .Title:         
0020: 20 44 69 63 74 20 47 65 74 20 57 69 74 68 20 44   Dict Get With D
0030: 65 66 61 75 6c 74 0d 56 65 72 73 69 6f 6e 3a 20  efault.Version: 
0040: 20 20 20 20 20 20 20 0d 41 75 74 68 6f 72 3a 20         .Author: 
0050: 20 20 20 20 20 20 20 20 4c 61 72 73 20 48 65 6c          Lars Hel
0060: 6c 73 74 72 c2 9a 6d 20 3c 4c 61 72 73 20 64 6f  lstršm <Lars do
0070: 74 20 48 65 6c 6c 73 74 72 6f 6d 20 61 74 20 72  t Hellstrom at r
0080: 65 73 69 64 65 6e 73 65 74 20 64 6f 74 20 6e 65  esidenset dot ne
0090: 74 3e 0d 53 74 61 74 65 3a 20 20 20 20 20 20 20  t>.State:       
00a0: 20 20 20 44 72 61 66 74 0d 54 79 70 65 3a 20 20     Draft.Type:  
00b0: 20 20 20 20 20 20 20 20 20 50 72 6f 6a 65 63 74           Project
00c0: 0d 56 6f 74 65 3a 20 20 20 20 20 20 20 20 20 20  .Vote:          
00d0: 20 0d 54 63 6c 2d 56 65 72 73 69 6f 6e 3a 20 20   .Tcl-Version:  
00e0: 20 20 38 2e 36 0d 43 72 65 61 74 65 64 3a 20 20    8.6.Created:  
00f0: 20 20 20 20 20 20 32 37 2d 4e 6f 76 2d 32 30 30        27-Nov-200
0100: 38 0d 4b 65 79 77 6f 72 64 73 3a 20 20 20 20 20  8.Keywords:     
0110: 20 20 64 69 63 74 2c 20 64 69 63 74 69 6f 6e 61    dict, dictiona
0120: 72 79 2c 20 64 65 66 61 75 6c 74 20 76 61 6c 75  ry, default valu
0130: 65 0d 50 6f 73 74 2d 48 69 73 74 6f 72 79 3a 20  e.Post-History: 
0140: 20 20 0d 0d 7e 20 41 62 73 74 72 61 63 74 0d 0d    ..~ Abstract..
0150: 41 20 6e 65 77 20 73 75 62 63 6f 6d 6d 61 6e 64  A new subcommand
0160: 20 6f 66 20 27 27 27 64 69 63 74 27 27 27 20 69   of '''dict''' i
0170: 73 20 70 72 6f 70 6f 73 65 64 2c 20 77 68 69 63  s proposed, whic
0180: 68 20 72 65 74 75 72 6e 73 20 61 20 64 69 63 74  h returns a dict
0190: 69 6f 6e 61 72 79 20 0d 76 61 6c 75 65 20 69 66  ionary .value if
01a0: 20 69 74 20 65 78 69 73 74 73 20 61 6e 64 20 72   it exists and r
01b0: 65 74 75 72 6e 73 20 61 20 70 65 72 2d 63 61 6c  eturns a per-cal
01c0: 6c 20 64 65 66 61 75 6c 74 20 6f 74 68 65 72 77  l default otherw
01d0: 69 73 65 2e 20 0d 0d 0d 7e 20 53 70 65 63 69 66  ise. ...~ Specif
01e0: 69 63 61 74 69 6f 6e 0d 0d 54 68 65 20 27 27 27  ication..The '''
01f0: 64 69 63 74 27 27 27 20 63 6f 6d 6d 61 6e 64 20  dict''' command 
0200: 77 69 6c 6c 20 67 65 74 20 61 20 6e 65 77 20 73  will get a new s
0210: 75 62 63 6f 6d 6d 61 6e 64 0d 0d 20 3e 20 27 27  ubcommand.. > ''
0220: 27 64 69 63 74 20 67 65 74 77 69 74 68 64 65 66  'dict getwithdef
0230: 61 75 6c 74 27 27 27 20 27 27 64 69 63 74 69 6f  ault''' ''dictio
0240: 6e 61 72 79 27 27 20 27 27 6b 65 79 27 27 20 3f  nary'' ''key'' ?
0250: 27 27 6b 65 79 27 27 20 2e 2e 2e 3f 0d 20 20 20  ''key'' ...?.   
0260: 27 27 76 61 6c 75 65 27 27 0d 0d 28 49 20 63 6f  ''value''..(I co
0270: 6e 73 69 64 65 72 20 74 68 65 20 6e 61 6d 65 20  nsider the name 
0280: 6f 66 20 74 68 69 73 20 73 75 62 63 6f 6d 6d 61  of this subcomma
0290: 6e 64 20 76 65 72 79 20 6d 75 63 68 20 6f 70 65  nd very much ope
02a0: 6e 20 66 6f 72 20 64 69 73 63 75 73 73 69 6f 6e  n for discussion
02b0: 29 20 0d 77 68 69 63 68 20 6d 6f 64 75 6c 6f 20  ) .which modulo 
02c0: 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 62  error messages b
02d0: 65 68 61 76 65 73 20 6c 69 6b 65 0d 0d 7c 20 70  ehaves like..| p
02e0: 72 6f 63 20 64 69 63 74 5f 67 65 74 77 69 74 68  roc dict_getwith
02f0: 64 65 66 61 75 6c 74 20 7b 44 20 61 72 67 73 7d  default {D args}
0300: 20 7b 0d 7c 20 20 20 20 20 69 66 20 7b 5b 64 69   {.|     if {[di
0310: 63 74 20 65 78 69 73 74 73 20 24 44 20 7b 2a 7d  ct exists $D {*}
0320: 5b 6c 72 61 6e 67 65 20 24 61 72 67 73 20 30 20  [lrange $args 0 
0330: 65 6e 64 2d 31 5d 5d 7d 20 74 68 65 6e 20 7b 0d  end-1]]} then {.
0340: 7c 20 20 20 20 20 20 20 20 20 64 69 63 74 20 67  |         dict g
0350: 65 74 20 24 44 20 7b 2a 7d 5b 6c 72 61 6e 67 65  et $D {*}[lrange
0360: 20 24 61 72 67 73 20 30 20 65 6e 64 2d 31 5d 0d   $args 0 end-1].
0370: 7c 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0d 7c  |     } else {.|
0380: 20 20 20 20 20 20 20 20 20 6c 69 6e 64 65 78 20           lindex 
0390: 24 61 72 67 73 20 65 6e 64 0d 7c 20 20 20 20 20  $args end.|     
03a0: 7d 0d 7c 20 7d 0d 0d 69 2e 65 2e 2c 20 69 74 20  }.| }..i.e., it 
03b0: 72 65 74 75 72 6e 73 20 74 68 65 20 76 61 6c 75  returns the valu
03c0: 65 20 66 72 6f 6d 20 74 68 65 20 27 27 64 69 63  e from the ''dic
03d0: 74 69 6f 6e 61 72 79 27 27 20 63 6f 72 72 65 73  tionary'' corres
03e0: 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 0d  ponding to the .
03f0: 73 65 71 75 65 6e 63 65 20 6f 66 20 27 27 6b 65  sequence of ''ke
0400: 79 27 27 73 20 69 66 20 69 74 20 65 78 69 73 74  y''s if it exist
0410: 73 2c 20 6f 72 20 74 68 65 20 64 65 66 61 75 6c  s, or the defaul
0420: 74 20 27 27 76 61 6c 75 65 27 27 20 6f 74 68 65  t ''value'' othe
0430: 72 77 69 73 65 2e 20 41 73 20 0d 77 69 74 68 20  rwise. As .with 
0440: 27 27 27 64 69 63 74 20 65 78 69 73 74 73 27 27  '''dict exists''
0450: 27 2c 20 69 74 20 69 73 20 4f 4b 20 28 61 6e 64  ', it is OK (and
0460: 20 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65 20   will cause the 
0470: 64 65 66 61 75 6c 74 20 27 27 76 61 6c 75 65 27  default ''value'
0480: 27 20 74 6f 20 0d 62 65 20 72 65 74 75 72 6e 65  ' to .be returne
0490: 64 29 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65  d) if one of the
04a0: 20 27 27 6b 65 79 27 27 73 20 69 73 20 6d 69 73   ''key''s is mis
04b0: 73 69 6e 67 20 66 72 6f 6d 20 69 74 73 20 64 69  sing from its di
04c0: 63 74 69 6f 6e 61 72 79 2c 20 62 75 74 20 0d 61  ctionary, but .a
04d0: 6e 20 65 72 72 6f 72 20 69 73 20 74 68 72 6f 77  n error is throw
04e0: 6e 20 69 66 20 74 68 69 73 20 70 61 74 68 20 6f  n if this path o
04f0: 66 20 6b 65 79 73 20 63 61 6e 6e 6f 74 20 62 65  f keys cannot be
0500: 20 74 72 61 76 65 72 73 65 64 20 62 65 63 61 75   traversed becau
0510: 73 65 20 74 68 65 20 0d 76 61 6c 75 65 20 61 73  se the .value as
0520: 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
0530: 65 20 70 72 65 76 69 6f 75 73 20 6b 65 79 20 69  e previous key i
0540: 73 20 6e 6f 74 20 61 20 64 69 63 74 69 6f 6e 61  s not a dictiona
0550: 72 79 2e 0d 0d 0d 7e 20 52 61 74 69 6f 6e 61 6c  ry....~ Rational
0560: 65 0d 0d 49 74 20 69 73 20 63 6c 65 61 72 20 74  e..It is clear t
0570: 68 61 74 20 67 65 74 74 69 6e 67 20 61 20 76 61  hat getting a va
0580: 6c 75 65 20 66 72 6f 6d 20 61 20 64 69 63 74 69  lue from a dicti
0590: 6f 6e 61 72 79 20 69 66 20 69 74 20 65 78 69 73  onary if it exis
05a0: 74 73 20 61 6e 64 20 75 73 69 6e 67 20 0d 61 20  ts and using .a 
05b0: 64 65 66 61 75 6c 74 20 6f 74 68 65 72 77 69 73  default otherwis
05c0: 65 20 69 73 20 61 20 63 6f 6d 6d 6f 6e 20 6f 70  e is a common op
05d0: 65 72 61 74 69 6f 6e 2c 20 62 75 74 20 69 74 20  eration, but it 
05e0: 69 73 20 61 6c 73 6f 20 63 6c 65 61 72 20 74 68  is also clear th
05f0: 61 74 20 74 68 69 73 20 0d 63 61 6e 20 62 65 20  at this .can be 
0600: 63 61 72 72 69 65 64 20 6f 75 74 20 77 69 74 68  carried out with
0610: 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f   a combination o
0620: 66 20 65 78 69 73 74 69 6e 67 20 54 63 6c 20 63  f existing Tcl c
0630: 6f 6d 6d 61 6e 64 73 2e 20 48 65 6e 63 65 20 74  ommands. Hence t
0640: 68 65 20 0d 69 73 73 75 65 20 69 73 20 77 68 65  he .issue is whe
0650: 74 68 65 72 20 61 20 6e 65 77 20 73 75 62 63 6f  ther a new subco
0660: 6d 6d 61 6e 64 20 66 6f 72 20 74 68 69 73 20 69  mmand for this i
0670: 6d 70 72 6f 76 65 73 20 65 66 66 69 63 69 65 6e  mproves efficien
0680: 63 79 20 61 6e 64 20 0d 63 6f 6e 76 65 6e 69 65  cy and .convenie
0690: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 70 65 72  nce of this oper
06a0: 61 74 69 6f 6e 20 65 6e 6f 75 67 68 20 74 6f 20  ation enough to 
06b0: 6a 75 73 74 69 66 79 20 74 68 65 20 70 6f 73 73  justify the poss
06c0: 69 62 6c 65 20 62 6c 6f 61 74 20 69 74 20 0d 62  ible bloat it .b
06d0: 72 69 6e 67 73 2e 0d 0d 0d 7e 7e 20 41 6c 74 65  rings....~~ Alte
06e0: 72 6e 61 74 69 76 65 20 4d 65 74 68 6f 64 73 0d  rnative Methods.
06f0: 0d 4f 6e 65 20 61 70 70 72 6f 61 63 68 20 74 68  .One approach th
0700: 61 74 20 68 61 73 20 62 65 65 6e 20 73 75 67 67  at has been sugg
0710: 65 73 74 65 64 20 66 6f 72 20 70 72 6f 76 69 64  ested for provid
0720: 69 6e 67 20 64 65 66 61 75 6c 74 20 76 61 6c 75  ing default valu
0730: 65 73 20 69 73 20 74 6f 20 0d 63 6f 6d 62 69 6e  es is to .combin
0740: 65 20 27 27 27 64 69 63 74 20 67 65 74 27 27 27  e '''dict get'''
0750: 20 77 69 74 68 20 27 27 27 64 69 63 74 20 6d 65   with '''dict me
0760: 72 67 65 27 27 27 2c 20 6c 69 6b 65 20 73 6f 3a  rge''', like so:
0770: 0d 0d 7c 20 20 64 69 63 74 20 67 65 74 20 5b 64  ..|  dict get [d
0780: 69 63 74 20 6d 65 72 67 65 20 7b 2d 61 70 61 20  ict merge {-apa 
0790: 62 61 72 7d 20 24 44 5d 20 2d 61 70 61 0d 0d 54  bar} $D] -apa..T
07a0: 68 69 73 20 61 70 70 72 6f 61 63 68 20 69 73 20  his approach is 
07b0: 68 6f 77 65 76 65 72 20 61 70 70 72 6f 70 72 69  however appropri
07c0: 61 74 65 20 6d 61 69 6e 6c 79 20 69 6e 20 73 69  ate mainly in si
07d0: 74 75 61 74 69 6f 6e 73 20 77 68 65 72 65 20 73  tuations where s
07e0: 65 76 65 72 61 6c 20 0d 6b 65 79 73 20 61 72 65  everal .keys are
07f0: 20 67 69 76 65 6e 20 66 69 78 65 64 20 64 65 66   given fixed def
0800: 61 75 6c 74 73 20 73 69 6d 75 6c 74 61 6e 65 6f  aults simultaneo
0810: 75 73 6c 79 2e 20 43 6f 6d 70 61 72 65 64 20 74  usly. Compared t
0820: 6f 20 0d 27 27 27 64 69 63 74 20 67 65 74 77 69  o .'''dict getwi
0830: 74 68 64 65 66 61 75 6c 74 27 27 27 2c 20 69 74  thdefault''', it
0840: 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69   has the followi
0850: 6e 67 20 64 69 73 61 64 76 61 6e 74 61 67 65 73  ng disadvantages
0860: 3a 0d 0d 20 20 20 2a 20 49 74 20 63 61 6e 6e 6f  :..   * It canno
0870: 74 20 62 65 20 75 73 65 64 20 66 6f 72 20 6b 65  t be used for ke
0880: 79 73 20 69 6e 20 6e 65 73 74 65 64 20 64 69 63  ys in nested dic
0890: 74 69 6f 6e 61 72 69 65 73 2e 0d 0d 20 20 20 2a  tionaries...   *
08a0: 20 49 74 20 74 61 6b 65 73 20 74 69 6d 65 20 70   It takes time p
08b0: 72 6f 70 6f 72 74 69 6f 6e 61 6c 20 74 6f 20 74  roportional to t
08c0: 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64  he size of the d
08d0: 69 63 74 69 6f 6e 61 72 79 2c 20 65 76 65 6e 20  ictionary, even 
08e0: 0d 20 20 20 20 20 77 68 65 6e 20 6f 6e 6c 79 20  .     when only 
08f0: 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 69 6e 73  one value is ins
0900: 70 65 63 74 65 64 2e 20 53 69 6e 63 65 20 27 27  pected. Since ''
0910: 27 64 69 63 74 20 66 69 6c 74 65 72 20 6b 65 79  'dict filter key
0920: 27 27 27 20 0d 20 20 20 20 20 68 61 73 20 61 6e  ''' .     has an
0930: 20 6f 70 74 69 6d 69 73 61 74 69 6f 6e 20 66 6f   optimisation fo
0940: 72 20 74 68 69 73 20 6b 69 6e 64 20 6f 66 20 73  r this kind of s
0950: 69 74 75 61 74 69 6f 6e 2c 20 74 68 65 72 65 20  ituation, there 
0960: 61 72 65 20 0d 20 20 20 20 20 61 70 70 61 72 65  are .     appare
0970: 6e 74 6c 79 20 6d 61 69 6e 74 61 69 6e 65 72 73  ntly maintainers
0980: 20 77 68 69 63 68 20 63 6f 6e 73 69 64 65 72 20   which consider 
0990: 73 75 63 68 20 64 69 66 66 65 72 65 6e 63 65 73  such differences
09a0: 20 72 65 6c 65 76 61 6e 74 2e 0d 0d 20 20 20 2a   relevant...   *
09b0: 20 54 68 65 20 22 6f 6e 65 20 27 27 27 64 69 63   The "one '''dic
09c0: 74 20 6d 65 72 67 65 27 27 27 20 65 61 72 6c 79  t merge''' early
09d0: 20 70 72 6f 76 69 64 69 6e 67 20 64 65 66 61 75   providing defau
09e0: 6c 74 73 20 66 6f 72 20 61 6c 6c 20 6b 65 79 73  lts for all keys
09f0: 22 20 0d 20 20 20 20 20 61 70 70 72 6f 61 63 68  " .     approach
0a00: 20 63 61 6e 6e 6f 74 20 64 65 61 6c 20 77 69 74   cannot deal wit
0a10: 68 20 6b 65 79 73 20 74 68 61 74 20 68 61 76 65  h keys that have
0a20: 20 64 79 6e 61 6d 69 63 20 64 65 66 61 75 6c 74   dynamic default
0a30: 73 2c 20 65 2e 67 2e 20 0d 20 20 20 20 20 74 68  s, e.g. .     th
0a40: 61 74 20 74 68 65 20 64 65 66 61 75 6c 74 20 66  at the default f
0a50: 6f 72 20 6f 70 74 69 6f 6e 20 2d 66 6f 6f 20 69  or option -foo i
0a60: 73 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20  s the effective 
0a70: 76 61 6c 75 65 20 6f 66 20 6f 70 74 69 6f 6e 0d  value of option.
0a80: 20 20 20 20 20 2d 62 61 72 2e 0d 0d 48 65 6e 63       -bar...Henc
0a90: 65 20 61 6c 74 68 6f 75 67 68 20 27 27 27 64 69  e although '''di
0aa0: 63 74 20 6d 65 72 67 65 27 27 27 20 69 73 20 73  ct merge''' is s
0ab0: 6f 6d 65 74 69 6d 65 73 20 61 70 70 72 6f 70 72  ometimes appropr
0ac0: 69 61 74 65 20 66 6f 72 20 70 72 6f 76 69 64 69  iate for providi
0ad0: 6e 67 20 0d 64 65 66 61 75 6c 74 73 2c 20 69 74  ng .defaults, it
0ae0: 20 69 73 20 6e 6f 74 20 61 20 75 6e 69 76 65 72   is not a univer
0af0: 73 61 6c 20 73 6f 6c 75 74 69 6f 6e 2e 0d 0d 54  sal solution...T
0b00: 68 65 20 62 61 73 69 63 20 61 70 70 72 6f 61 63  he basic approac
0b10: 68 20 69 73 20 69 6e 73 74 65 61 64 20 74 6f 2c  h is instead to,
0b20: 20 61 73 20 69 6e 20 74 68 65 20 27 27 27 64 69   as in the '''di
0b30: 63 74 5f 67 65 74 77 69 74 68 64 65 66 61 75 6c  ct_getwithdefaul
0b40: 74 27 27 27 20 0d 70 72 6f 63 20 61 62 6f 76 65  t''' .proc above
0b50: 2c 20 66 69 72 73 74 20 75 73 65 20 27 27 27 64  , first use '''d
0b60: 69 63 74 20 65 78 69 73 74 73 27 27 27 20 61 6e  ict exists''' an
0b70: 64 20 74 68 65 6e 20 27 27 27 64 69 63 74 20 67  d then '''dict g
0b80: 65 74 27 27 27 20 69 66 20 74 68 65 20 0d 76 61  et''' if the .va
0b90: 6c 75 65 20 65 78 69 73 74 65 64 2e 20 50 72 6f  lue existed. Pro
0ba0: 62 6c 65 6d 73 20 77 69 74 68 20 74 68 69 73 20  blems with this 
0bb0: 61 70 70 72 6f 61 63 68 20 61 72 65 3a 0d 0d 20  approach are:.. 
0bc0: 20 20 2a 20 49 74 20 69 73 20 72 65 64 75 6e 64    * It is redund
0bd0: 61 6e 74 3a 20 61 6c 72 65 61 64 79 20 27 27 27  ant: already '''
0be0: 64 69 63 74 20 65 78 69 73 74 73 27 27 27 20 72  dict exists''' r
0bf0: 65 74 72 69 65 76 65 73 20 74 68 65 20 76 61 6c  etrieves the val
0c00: 75 65 2c 20 0d 20 20 20 20 20 62 75 74 20 64 6f  ue, .     but do
0c10: 65 73 6e 27 74 20 72 65 74 75 72 6e 20 69 74 2c  esn't return it,
0c20: 20 73 6f 20 27 27 27 64 69 63 74 20 67 65 74 27   so '''dict get'
0c30: 27 27 20 68 61 73 20 74 6f 20 6c 6f 6f 6b 20 69  '' has to look i
0c40: 74 20 75 70 20 61 6c 6c 20 0d 20 20 20 20 20 6f  t up all .     o
0c50: 76 65 72 20 61 67 61 69 6e 2e 0d 0d 20 20 20 2a  ver again...   *
0c60: 20 49 74 20 69 73 20 62 75 6c 6b 79 3a 20 69 66   It is bulky: if
0c70: 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 64 69   the value in di
0c80: 63 74 69 6f 6e 61 72 79 20 27 27 44 27 27 20 6f  ctionary ''D'' o
0c90: 66 20 6f 70 74 69 6f 6e 20 27 27 27 2d 61 70 61  f option '''-apa
0ca0: 27 27 27 20 0d 20 20 20 20 20 28 6f 72 20 69 74  ''' .     (or it
0cb0: 73 20 64 65 66 61 75 6c 74 20 27 27 27 62 61 72  s default '''bar
0cc0: 27 27 27 29 20 69 73 20 74 6f 20 62 65 20 70 61  ''') is to be pa
0cd0: 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d  ssed as an argum
0ce0: 65 6e 74 20 74 6f 20 74 68 65 20 0d 20 20 20 20  ent to the .    
0cf0: 20 63 6f 6d 6d 61 6e 64 20 27 27 27 66 6f 6f 27   command '''foo'
0d00: 27 27 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6d  '', then the com
0d10: 70 6c 65 74 65 20 63 6f 6d 6d 61 6e 64 20 69 73  plete command is
0d20: 0d 0d 7c 20 20 20 20 20 66 6f 6f 20 5b 69 66 20  ..|     foo [if 
0d30: 7b 5b 64 69 63 74 20 65 78 69 73 74 73 20 24 44  {[dict exists $D
0d40: 20 2d 61 70 61 5d 7d 20 74 68 65 6e 20 7b 64 69   -apa]} then {di
0d50: 63 74 20 67 65 74 20 24 44 20 2d 61 70 61 7d 5c  ct get $D -apa}\
0d60: 0d 7c 20 20 20 20 20 20 20 65 6c 73 65 20 7b 72  .|       else {r
0d70: 65 74 75 72 6e 20 2d 6c 65 76 65 6c 20 30 20 62  eturn -level 0 b
0d80: 61 72 7d 5d 0d 0d 20 20 20 3e 20 6f 72 20 0d 0d  ar}]..   > or ..
0d90: 7c 20 20 20 20 20 66 6f 6f 20 5b 65 78 70 72 20  |     foo [expr 
0da0: 7b 5b 64 69 63 74 20 65 78 69 73 74 73 20 24 44  {[dict exists $D
0db0: 20 2d 61 70 61 5d 20 3f 20 5b 64 69 63 74 20 67   -apa] ? [dict g
0dc0: 65 74 20 24 44 20 2d 61 70 61 5d 20 3a 20 22 62  et $D -apa] : "b
0dd0: 61 72 22 7d 5d 0d 0d 20 20 20 3e 20 77 68 69 63  ar"}]..   > whic
0de0: 68 20 6d 61 6e 79 20 70 72 6f 67 72 61 6d 6d 65  h many programme
0df0: 72 73 20 77 6f 75 6c 64 20 66 69 6e 64 20 6f 62  rs would find ob
0e00: 6a 65 63 74 69 6f 6e 61 62 6c 65 2e 20 54 68 65  jectionable. The
0e10: 0d 20 20 20 20 20 27 27 27 64 69 63 74 20 67 65  .     '''dict ge
0e20: 74 77 69 74 68 64 65 66 61 75 6c 74 27 27 27 20  twithdefault''' 
0e30: 63 6f 75 6e 74 65 72 70 61 72 74 20 69 73 20 6d  counterpart is m
0e40: 65 72 65 6c 79 0d 0d 7c 20 20 20 20 20 66 6f 6f  erely..|     foo
0e50: 20 5b 64 69 63 74 20 67 65 74 77 69 74 68 64 65   [dict getwithde
0e60: 66 61 75 6c 74 20 24 44 20 2d 61 70 61 20 62 61  fault $D -apa ba
0e70: 72 5d 0d 0d 54 68 65 20 6f 6e 6c 79 20 77 61 79  r]..The only way
0e80: 20 74 6f 20 61 76 6f 69 64 20 74 68 65 20 72 65   to avoid the re
0e90: 64 75 6e 64 61 6e 63 65 20 6f 66 20 61 6e 20 65  dundance of an e
0ea0: 78 74 72 61 20 6c 6f 6f 6b 2d 75 70 20 73 65 65  xtra look-up see
0eb0: 6d 73 20 74 6f 20 62 65 20 74 6f 20 0d 63 6f 6d  ms to be to .com
0ec0: 62 69 6e 65 20 27 27 27 64 69 63 74 20 67 65 74  bine '''dict get
0ed0: 27 27 27 20 77 69 74 68 20 27 27 27 63 61 74 63  ''' with '''catc
0ee0: 68 27 27 27 2c 20 6c 69 6b 65 20 73 6f 3a 0d 0d  h''', like so:..
0ef0: 7c 20 20 69 66 20 7b 5b 63 61 74 63 68 20 7b 64  |  if {[catch {d
0f00: 69 63 74 20 67 65 74 20 24 44 20 2d 61 70 61 7d  ict get $D -apa}
0f10: 20 76 61 6c 75 65 5d 7d 20 74 68 65 6e 20 7b 73   value]} then {s
0f20: 65 74 20 76 61 6c 75 65 20 62 61 72 7d 20 65 6c  et value bar} el
0f30: 73 65 20 7b 73 65 74 20 76 61 6c 75 65 7d 0d 0d  se {set value}..
0f40: 62 75 74 20 74 68 69 73 20 68 61 73 20 74 68 65  but this has the
0f50: 20 64 69 73 61 64 76 61 6e 74 61 67 65 20 6f 66   disadvantage of
0f60: 20 68 69 64 69 6e 67 20 6f 74 68 65 72 20 73 6f   hiding other so
0f70: 75 72 63 65 73 20 6f 66 20 65 72 72 6f 72 2c 20  urces of error, 
0f80: 73 75 63 68 20 61 73 20 0d 27 27 44 27 27 20 6e  such as .''D'' n
0f90: 6f 74 20 62 65 69 6e 67 20 61 20 64 69 63 74 69  ot being a dicti
0fa0: 6f 6e 61 72 79 20 69 6e 20 74 68 65 20 66 69 72  onary in the fir
0fb0: 73 74 20 70 6c 61 63 65 2e 20 54 68 69 73 20 6b  st place. This k
0fc0: 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 69 6e 20  ind of error in 
0fd0: 0d 61 20 6e 6f 72 6d 61 6c 20 70 72 6f 63 65 73  .a normal proces
0fe0: 73 69 6e 67 20 70 61 74 68 20 69 73 20 61 6c 73  sing path is als
0ff0: 6f 20 63 6f 6e 73 69 64 65 72 65 64 20 70 6f 6f  o considered poo
1000: 72 20 73 74 79 6c 65 20 62 79 20 73 6f 6d 65 2e  r style by some.
1010: 0d 0d 0d 7e 7e 20 49 6d 70 6c 65 6d 65 6e 74 61  ...~~ Implementa
1020: 74 69 6f 6e 20 43 68 6f 69 63 65 73 0d 0d 45 76  tion Choices..Ev
1030: 65 6e 20 69 66 20 69 74 20 69 73 20 64 65 65 6d  en if it is deem
1040: 65 64 20 61 70 70 72 6f 70 72 69 61 74 65 20 74  ed appropriate t
1050: 6f 20 68 61 76 65 20 61 20 64 65 64 69 63 61 74  o have a dedicat
1060: 65 64 20 73 75 62 63 6f 6d 6d 61 6e 64 20 6f 66  ed subcommand of
1070: 20 0d 27 27 27 64 69 63 74 27 27 27 20 66 6f 72   .'''dict''' for
1080: 20 74 68 69 73 2c 20 69 74 20 63 6f 75 6c 64 20   this, it could 
1090: 62 65 20 61 72 67 75 65 64 20 74 68 61 74 20 69  be argued that i
10a0: 74 20 6e 65 65 64 6e 27 74 20 62 65 20 70 61 72  t needn't be par
10b0: 74 20 6f 66 20 74 68 65 20 0d 63 6f 6d 70 69 6c  t of the .compil
10c0: 65 64 20 54 63 6c 20 63 6f 72 65 3b 20 73 69 6e  ed Tcl core; sin
10d0: 63 65 20 27 27 27 64 69 63 74 27 27 27 20 69 73  ce '''dict''' is
10e0: 20 61 6e 20 65 6e 73 65 6d 62 6c 65 2c 20 61 6e   an ensemble, an
10f0: 79 6f 6e 65 20 63 61 6e 20 65 78 74 65 6e 64 20  yone can extend 
1100: 69 74 20 0d 61 74 20 74 68 65 20 73 63 72 69 70  it .at the scrip
1110: 74 20 6c 65 76 65 6c 20 61 6e 64 20 22 74 68 65  t level and "the
1120: 20 63 6f 72 65 20 63 61 6e 20 64 6f 20 77 69 74   core can do wit
1130: 68 6f 75 74 20 74 68 69 73 20 62 6c 6f 61 74 22  hout this bloat"
1140: 2e 20 48 6f 77 65 76 65 72 2c 20 69 74 20 0d 74  . However, it .t
1150: 75 72 6e 73 20 6f 75 74 20 74 68 61 6e 20 61 6e  urns out than an
1160: 20 69 6e 2d 63 6f 72 65 20 69 6d 70 6c 65 6d 65   in-core impleme
1170: 6e 74 61 74 69 6f 6e 20 69 73 20 76 65 72 79 20  ntation is very 
1180: 65 61 73 79 20 77 68 65 72 65 61 73 20 74 68 65  easy whereas the
1190: 20 0d 61 6c 74 65 72 6e 61 74 69 76 65 73 20 61   .alternatives a
11a0: 72 65 20 6e 6f 74 20 73 6f 20 65 61 73 79 2e 0d  re not so easy..
11b0: 0d 43 6f 6e 63 72 65 74 65 6c 79 2c 20 74 68 65  .Concretely, the
11c0: 20 6e 65 63 65 73 73 61 72 79 20 44 69 63 74 47   necessary DictG
11d0: 65 74 57 69 74 68 44 65 66 61 75 6c 74 43 6d 64  etWithDefaultCmd
11e0: 20 69 73 20 61 20 74 72 69 76 69 61 6c 20 6d 6f   is a trivial mo
11f0: 64 69 66 69 63 61 74 69 6f 6e 20 0d 6f 66 20 44  dification .of D
1200: 69 63 74 45 78 69 73 74 73 43 6d 64 2c 20 74 6f  ictExistsCmd, to
1210: 20 74 61 6b 65 20 6f 6e 65 20 65 78 74 72 61 20   take one extra 
1220: 61 72 67 75 6d 65 6e 74 20 61 66 74 65 72 20 74  argument after t
1230: 68 65 20 27 27 6b 65 79 27 27 73 20 61 6e 64 20  he ''key''s and 
1240: 63 68 61 6e 67 65 20 0d 74 68 65 20 66 69 6e 61  change .the fina
1250: 6c 0d 0d 7c 20 20 54 63 6c 5f 53 65 74 4f 62 6a  l..|  Tcl_SetObj
1260: 52 65 73 75 6c 74 28 69 6e 74 65 72 70 2c 20 54  Result(interp, T
1270: 63 6c 5f 4e 65 77 42 6f 6f 6c 65 61 6e 4f 62 6a  cl_NewBooleanObj
1280: 28 76 61 6c 75 65 50 74 72 20 21 3d 20 4e 55 4c  (valuePtr != NUL
1290: 4c 29 29 3b 0d 0d 74 6f 0d 0d 7c 20 20 54 63 6c  L));..to..|  Tcl
12a0: 5f 53 65 74 4f 62 6a 52 65 73 75 6c 74 28 69 6e  _SetObjResult(in
12b0: 74 65 72 70 2c 20 76 61 6c 75 65 50 74 72 20 21  terp, valuePtr !
12c0: 3d 20 4e 55 4c 4c 20 3f 20 76 61 6c 75 65 50 74  = NULL ? valuePt
12d0: 72 20 3a 20 6f 62 6a 76 5b 6f 62 6a 63 2d 31 5d  r : objv[objc-1]
12e0: 29 3b 0d 0d 49 74 20 69 73 20 6e 6f 77 68 65 72  );..It is nowher
12f0: 65 20 6e 65 61 72 20 61 73 20 65 61 73 79 20 74  e near as easy t
1300: 6f 20 64 6f 20 74 68 69 73 20 69 6e 20 61 20 77  o do this in a w
1310: 65 6c 6c 2d 62 65 68 61 76 65 64 20 65 78 74 65  ell-behaved exte
1320: 6e 73 69 6f 6e 2c 20 73 69 6e 63 65 20 0d 44 69  nsion, since .Di
1330: 63 74 45 78 69 73 74 73 43 6d 64 20 72 65 6c 69  ctExistsCmd reli
1340: 65 73 20 6f 6e 20 54 63 6c 54 72 61 63 65 44 69  es on TclTraceDi
1350: 63 74 50 61 74 68 20 74 6f 20 64 6f 20 6d 6f 73  ctPath to do mos
1360: 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2c 20 61  t of the work, a
1370: 6e 64 20 0d 74 68 65 20 6c 61 74 74 65 72 20 69  nd .the latter i
1380: 73 20 41 46 41 49 43 54 20 61 74 20 62 65 73 74  s AFAICT at best
1390: 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68   available in th
13a0: 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 75 62 73  e internal stubs
13b0: 20 74 61 62 6c 65 2e 0d 0d 41 20 73 63 72 69 70   table...A scrip
13c0: 74 2d 6c 65 76 65 6c 20 69 6d 70 6c 65 6d 65 6e  t-level implemen
13d0: 74 61 74 69 6f 6e 20 69 73 20 63 65 72 74 61 69  tation is certai
13e0: 6e 6c 79 20 70 6f 73 73 69 62 6c 65 2c 20 62 75  nly possible, bu
13f0: 74 20 74 68 65 20 6d 69 6e 75 74 65 20 0d 64 65  t the minute .de
1400: 74 61 69 6c 73 20 6f 66 20 70 72 6f 64 75 63 69  tails of produci
1410: 6e 67 20 63 6f 72 65 2d 6c 6f 6f 6b 69 6e 67 20  ng core-looking 
1420: 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69  error messages i
1430: 6e 20 74 68 69 73 20 63 61 73 65 20 61 70 70 65  n this case appe
1440: 61 72 73 20 0d 63 6f 6e 73 69 64 65 72 61 62 6c  ars .considerabl
1450: 65 20 62 6f 74 68 20 63 6f 6d 70 61 72 65 64 20  e both compared 
1460: 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61  to the functiona
1470: 6c 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 63  l parts of the c
1480: 6f 6d 6d 61 6e 64 20 61 6e 64 20 0d 63 6f 6d 70  ommand and .comp
1490: 61 72 65 64 20 74 6f 20 74 68 65 20 61 6d 6f 75  ared to the amou
14a0: 6e 74 20 6f 66 20 63 6f 64 65 20 6e 65 65 64 65  nt of code neede
14b0: 64 20 74 6f 20 64 6f 20 69 74 20 69 6e 20 74 68  d to do it in th
14c0: 65 20 63 6f 72 65 2e 0d 0d 0d 7e 20 52 65 66 65  e core....~ Refe
14d0: 72 65 6e 63 65 20 49 6d 70 6c 65 6d 65 6e 74 61  rence Implementa
14e0: 74 69 6f 6e 0d 0d 41 6e 20 69 6d 70 6c 65 6d 65  tion..An impleme
14f0: 6e 74 61 74 69 6f 6e 20 69 73 20 70 72 6f 76 69  ntation is provi
1500: 64 65 64 20 6f 6e 20 53 46 2c 20 69 6e 20 70 61  ded on SF, in pa
1510: 74 63 68 20 23 32 33 37 30 35 37 35 2e 0d 0d 0d  tch #2370575....
1520: 7e 20 43 6f 70 79 72 69 67 68 74 0d 0d 54 68 69  ~ Copyright..Thi
1530: 73 20 64 6f 63 75 6d 65 6e 74 20 68 61 73 20 62  s document has b
1540: 65 65 6e 20 70 6c 61 63 65 64 20 69 6e 20 74 68  een placed in th
1550: 65 20 70 75 62 6c 69 63 20 64 6f 6d 61 69 6e 2e  e public domain.
1560: 20 0d                                             .