Version 1 of isDict

Updated 2018-07-22 14:01:00 by PeterLewerin

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".