Posted to tcl by sebres at Fri Sep 21 17:12:49 GMT 2018view raw

  1. In my opinion, the certain "immutability" of dict + EIAS (as well as the absence of weak/smart pointers) avoid the normal usage of the dict as real branch-tree (at least updatable one, e. g. if navigating over tree branches, etc)...
  2.  
  3. I have own native tcl-object-types in TclSE (struct, record, etc), that are dict-similar, where one can do things like:
  4.  
  5. % set t [new record from json {"a":{"aa":1,"ab":2},"b":{"ba":3,"bb":4},"c":{"ca":5,"cb":6}}]
  6.  
  7. % $t as string
  8. a {aa 1 ab 2} b {ba 3 bb 4} c {ca 5 cb 6}
  9. % $t as json -pretty
  10. {
  11. "a" : {
  12. "aa" : 1,
  13. "ab" : 2
  14. },
  15. "b" : {
  16. "ba" : 3,
  17. "bb" : 4
  18. },
  19. "c" : {
  20. "ca" : 5,
  21. "cb" : 6
  22. }
  23. }
  24.  
  25. % set b [$t . b]
  26.  
  27. % $b set bc 5
  28. % $t as string
  29. a {aa 1 ab 2} b {ba 3 bb 4 bc 5} c {ca 5 cb 6}
  30.  
  31. % $b clear
  32. % $t as string
  33. a {aa 1 ab 2} b {} c {ca 5 cb 6}
  34.  
  35. % $b set x x
  36. % proc some-proc {branch args} { $branch update $args }
  37. % some-proc $b y Y z Z
  38. % $t as string
  39. a {aa 1 ab 2} b {x x y Y z Z} c {ca 5 cb 6}
  40.  
  41. % $t . b unset z
  42. % $b as string
  43. y Y
  44.  
  45. % $b record z { bza 8 bzb 9 }
  46. % $b . z set "a&c" [list [$t . a] [$t . c]]
  47. % [lindex [$b . z . "a&c"] 0] set ac 3
  48. % [lindex [$b . z . "a&c"] 1] set cc 7
  49.  
  50. % $t as json -pretty
  51. {
  52. "a" : {
  53. "aa" : 1,
  54. "ab" : 2,
  55. "ac" : 3
  56. },
  57. "b" : {
  58. "x" : "x",
  59. "y" : "Y",
  60. "z" : {
  61. "bza" : 8,
  62. "bzb" : 9,
  63. "a&c" : [
  64. {
  65. "aa" : 1,
  66. "ab" : 2,
  67. "ac" : 3
  68. },
  69. {
  70. "ca" : 5,
  71. "cb" : 6,
  72. "cc" : 7
  73. }
  74. ]
  75. }
  76. },
  77. "c" : {
  78. "ca" : 5,
  79. "cb" : 6,
  80. "cc" : 7
  81. }
  82. }
  83.  
  84. Simply try to do the same using tcl dict's.
  85.