Huddle is a way of encoding Tcl values with structure annotations; it's conceptually similar to JSON and YAML, but different in detail.
The major issue you face is that a {b}
is not a canonical list or dictionary (though it is indeed both a valid list and a valid dictionary). That is, you'll never get it as a result from list
or dict create
(or any of the other structured-value-synthesising commands). To get it, you'll have to make the value more directly:
set val [format "%s {%s}" a b]
Once you've got the value, putting it in a Huddle list will encapsulate it just fine:
set hud [huddle list [format "%s {%s}" a b] [format "%s {%s}" c d]]
And then you can strip it to get the value you want:
puts [huddle strip $hud]
#==> {a {b}} {c {d}}
Advanced Construction
Forcing the addition of the braces when you don't “need” them is quite tricky if you want Tcl to otherwise do things right. The simplest (raw) mechanism is this:
if {[list $val] eq $val} {format "{%s}" $val} {list $val}
You need to do it like this because otherwise you'll go thoroughly wrong on values which are not barewords. (This is particularly true if you've got unbalanced braces in the value; in that case, the results are ugly due to the addition of backslashes but they work.)
With that, we can then construct the value to use in Huddle like this:
proc braceitem val {
if {[list $val] eq $val} {format "{%s}" $val} {list $val}
}
set val [concat [list a] [braceitem b]]
Most Tcl programmers don't do this. It's a lot of work and the code is a lot more verbose. It's also more error-prone than just using list
(or dict create
) and accepting formatting it uses by default.