Version 0 of pure value

Updated 2016-01-13 19:10:06 by pooryorick

A pure value is a value for which no string representation has yet been generated.

Description

In Tcl, every value is a string, but for efficiency, Tcl only generates those strings as needed. Take, for example,

set a [expr {2 ** 4}]

Here, $a has been given a value that was created in a numeric context, and the value of $a has not actually been used yet, so there has been no reason so far for Tcl to go ahead and generate a string representation of the number 16. Even when $a is passed to another command, there may still be no need to generate the string representation:

lappend list $a

Tcl is perfectly capable of appending $a to a list without looking to see what the string representation of $a might be, so after lappend, $a is still pure. It has no string representation. Since the value of $a came from a numeric context, and Tcl internally cached a numeric representation of $a, it can be passed back into a numeric context, and used efficiently, and still, no string representation is generated:

set b [expr {$a ** 2}]

At this point, neither $a or $b have a string representation. This can be verified with ::tcl::unsupported::representation:

puts [::tcl::unsupported::representation $a]
 value is a int with a refcount of 3, object pointer at 0x600064550, internal representation 0x10:0x600064580, no string representation

At some point, Tcl may decide it needs a string representation of the value. One such point is when the value is used as the key in a dictionary:

dict set dict $a something
puts [::tcl::unsupported::representation $a]
 value is a int with a refcount of 4, object pointer at 0x600064550, internal representation 0x10:0x600064580, string representation "16"

Tcl is a scripting language, after all, so usually it's not necessary to worry about exactly what internal or string representations Tcl has generated for a value, but sometimes it can make a big difference, typically when working with a large list or dictionary.

See Also

pure list
pure numbers
Tcl Performance
A guide to this and other such Tcl performance tips.