Created by CecilWesterhof.
Default you cannot check if something is a dict, That is why I thought it a good idea to write a proc for it.
A simple check is to check if something is a list and if so if the list contains an even number of elements.
But I find this not good enough, because this would validate:
{[} \] {[} \]
In my opinion this is not a correct dict. (Opinions differ about this.) It can certainly not be created with dict operations. And when doing:
dict set d \{ }
The list is cleaned up and you get:
{[} \] \{ \}
That is why I check if the length of the list is two times the number of the keys. The code becomes:
proc isDict {d} { expr {[string is list $d] && \ ! ([llength $d] % 2) && \ ((2 * [llength [dict keys $d]]) == [llength $d])} }
As always: comments, tips and questions are appreciated.
PL 2018-07-22: see also TIP 501 "string is dict".
EMJ 2018-07-22: which I think would also validate the above four-element list.
EMJ 2018-07-22: Put the original four-element list above in a variable and you can use that variable as a dict. Of course the duplicate key disappears because that is a property of dicts (and if the values were different it is the last which would be taken).
Also the proc above will error if fed, for example, a three-element list, so you need a catch or try in there.
CecilWesterhof 2018-07-23: No, it will go OK:
$ set d [list a b c] a b c $ isDict $d 0
PYK 2018-07-23: This topic is also discussed at dict tips and tricks.