dict create

dict create ?key value? ?key value? ...

Creates a dictionary out of a sequence of keys and values passed as individual arguments. If a key is repeated, it shall be in the resulting dictionary in a location that corresponds to the first time that key occurs, but shall use the last value for that key.


Empty Dictionaries

Recently on comp.lang.tcl, someone asked how to create an empty dict, and the reply was, basically,

set e [dict create]

Certainly there are other ways to set the variable to the equivalent of an empty dict

set e ""
set e [list]
set e {}
set e []

However, these latter methods don't convey to the reader of the program that you are expecting to perform dict operations on the variable. Probably not a big deal, though.

DKF: They're all likely to have exactly the same effect in practice anyway: storing a shared empty object in e.

AMG: Not quite. Unlike the other practically equivalent methods shown above, zero-argument [dict create] will actually make an object with a empty dict internal representation.

% tcl::unsupported::representation ""
value is a bytecode with a refcount of 22, [...], string representation "".
% tcl::unsupported::representation [list]
value is a pure string with a refcount of 1, [...], string representation "".
% tcl::unsupported::representation {}
value is a bytecode with a refcount of 23, [...], string representation "".
% tcl::unsupported::representation []
value is a pure string with a refcount of 2, [...], string representation "".
% tcl::unsupported::representation [dict create]
value is a dict with a refcount of 1, [...], no string representation.

"" and {} always give the same pointers as each other, across multiple invocations of the above. Each invocation of [list], [], and [dict create] gives different pointers. [] always has a refcount of 2, despite changing its pointer.

This is with Tcl 8.6b1.2.


See Also