ZetCode

Tcl values and types

last modified November 24, 2026 Tcl 8.6

Every value in Tcl is a string. A variable has no type of its own; it holds a string, and commands such as expr, llength or dict get interpret that string when they need a number, a list or a dictionary. The same value can act as an integer in one line and as plain text in the next.

This article looks at the value model from the outside in: how to ask a string what it can be used as, how numbers and booleans are written, how lists and dictionaries live inside ordinary strings, and what happens when the interpreter converts one form into another. The set and expr pages hold the full command reference.

Values are strings

The set command stores a string and returns it unchanged. The first script stores three values and inspects them with string and expr; nothing is declared, so only the commands decide what the characters mean.

values.tcl
set count 42
set price 19.95
set name Jane

puts "count: $count"
puts "price: $price"
puts "name:  $name"
puts "integer? [string is integer $count]"
puts "length of count: [string length $count]"
puts "count + 8 = [expr {$count + 8}]"

puts prints the string form. string is integer reads the characters and returns 1 for 42. The last line passes the same string to expr, which converts it, adds eight and returns a new string. string length reports two characters because length is measured on the text, not on the number.

$ tclsh values.tcl
count: 42
price: 19.95
name:  Jane
integer? 1
length of count: 2
count + 8 = 50

Checking values with string is

string is answers whether a value could be used as a number, a boolean or a list, without converting it. The syntax is string is class ?-strict? value. Tcl 8.6 has integer, double, boolean, list and character classes, but no dict class and no empty class, so the script tests those two separately.

classify.tcl
proc describe {value} {
    set flags {}
    foreach type {integer double boolean list} {
        if {[string is $type -strict $value]} {
            lappend flags $type
        }
    }
    if {![catch {dict size $value}]} {
        lappend flags dict
    }
    if {[string length $value] == 0} {
        lappend flags empty
    }
    return [format "%-12s %s" [list $value] [join $flags {, }]]
}

foreach value {42 -7 3.14 1e3 true no {a b c} {x 1 y 2} {} 007} {
    puts [describe $value]
}

The classes overlap: 42 is an integer, a double and a one-element list, and {} is an empty list and an empty dictionary. The catch command runs a script and returns 1 when it fails, the only way in 8.6 to ask whether a value is a well-formed dictionary. Without -strict, string is integer "" returns 1; with it, the empty string fails every class test.

$ tclsh classify.tcl
42           integer, double, list
-7           integer, double, list
3.14         double, list
1e3          double, list
true         boolean, list
no           boolean, list
{a b c}      list
{x 1 y 2}    list, dict
{}           list, dict, empty
007          integer, double, list

Numbers and formats

Tcl 8.6 accepts decimal, 0x hexadecimal, 0o octal and 0b binary literals. The old 0-prefixed octal form still works but is gone in Tcl 9. The script converts each literal with expr and writes values back out with format.

numbers.tcl
set decimal 255
set hex 0xFF
set octal 0o377
set binary 0b11111111
set oldoctal 0377
set exponent 1.5e3

puts "hex 0xFF          = [expr {$hex}]"
puts "octal 0o377       = [expr {$octal}]"
puts "binary 0b11111111 = [expr {$binary}]"
puts "old octal 0377    = [expr {$oldoctal}]"
puts "exponent 1.5e3    = [expr {$exponent}]"
puts "format %#x 255    = [format %#x $decimal]"
puts "format %#o 255    = [format %#o $decimal]"
puts "format %010.3f    = [format %010.3f $exponent]"
puts "10 / 4            = [expr {10 / 4}]"
puts "10 / 4.0          = [expr {10 / 4.0}]"
puts "2**62             = [expr {2**62}]"
puts "2**63             = [expr {2**63}]"
puts "2**63 - 1         = [expr {2**63 - 1}]"

All four spellings of 255 give the same integer, while the exponent form gives a double, so 1.5e3 prints as 1500.0. 10 / 4 truncates to 2 but 10 / 4.0 gives 2.5, and 2**63 is promoted to an arbitrary-precision integer instead of overflowing. %#x adds the 0x prefix, and %010.3f pads to ten characters with three decimals; the format page lists every conversion.

$ tclsh numbers.tcl
hex 0xFF          = 255
octal 0o377       = 255
binary 0b11111111 = 255
old octal 0377    = 255
exponent 1.5e3    = 1500.0
format %#x 255    = 0xff
format %#o 255    = 0377
format %010.3f    = 001500.000
10 / 4            = 2
10 / 4.0          = 2.5
2**62             = 4611686018427387904
2**63             = 9223372036854775808
2**63 - 1         = 9223372036854775807

Booleans

A boolean is a string that expr and if can read: 0 and 1, true and false, yes and no, or on and off, in any case. Other integers are true in expr, but the strict checker rejects them.

booleans.tcl
set samples {1 0 true false yes no on off 42 -3 maybe}

foreach value $samples {
    if {[string is boolean -strict $value]} {
        set result [expr {$value ? 1 : 0}]
        puts [format "%-6s boolean, evaluates to %s" $value $result]
    } else {
        puts [format "%-6s not a strict boolean" $value]
    }
}

puts "string is boolean 42: [string is boolean 42]"
puts "expr treats 42 as:    [expr {42 ? "true" : "false"}]"

The eight documented spellings pass and convert to one or zero. 42 and maybe do not, yet the last line shows that expr accepts 42 as true: the checker validates text while expr accepts anything it can read as a number. Configuration files want the strict spelling, so a typo such as ture is reported instead of being treated as true.

$ tclsh booleans.tcl
1      boolean, evaluates to 1
0      boolean, evaluates to 0
true   boolean, evaluates to 1
false  boolean, evaluates to 0
yes    boolean, evaluates to 1
no     boolean, evaluates to 0
on     boolean, evaluates to 1
off    boolean, evaluates to 0
42     not a strict boolean
-3     not a strict boolean
maybe  not a strict boolean
string is boolean 42: 0
expr treats 42 as:    true

Lists and dictionaries as ordinary values

A list is a string of words separated by whitespace, with braces grouping words that contain spaces. A dictionary is a list with an even number of elements, read as key and value pairs. Neither is a container object; the script reads one string both ways.

containers.tcl
set record {name Jane age 28 city Bratislava}
puts "as a list:  [llength $record] elements"
puts "first pair: [lrange $record 0 1]"
puts "as a dict:  [dict size $record] keys"
puts "name:       [dict get $record name]"

set d [dict create name Jane note {likes tea}]
puts "dict prints as: $d"
puts "list prints as: [list name Jane note {likes tea}]"

The string has six words, so llength returns six, while dict size reads three pairs. Both work on the same value without changing it. The last lines show the canonical form: a word with a space is braced, and a dictionary and a list produce identical text. What a value is depends on the command reading it, which is why the list and dict pages belong together.

$ tclsh containers.tcl
as a list:  6 elements
first pair: name Jane
as a dict:  3 keys
name:       Jane
dict prints as: name Jane note {likes tea}
list prints as: name Jane note {likes tea}

Internal representations and shimmering

The interpreter caches an internal form for values used as lists, dictionaries, integers or doubles. When a command needs a form the value lacks, Tcl parses the string, stores the new form and discards the old one; this is shimmering. tcl::unsupported::representation shows the current form.

shimmer.tcl
proc kind {value} {
    set text [tcl::unsupported::representation $value]
    regexp {value is an? (?:pure )?(\w+)} $text -> type
    return $type
}

set data "10 20 30"
puts "data starts as:  [kind $data]"
puts "three elements:  [llength $data]"
puts "data is now a:   [kind $data]"

set n 42
puts "42 starts as:    [kind $n]"
puts "42 doubled:      [expr {$n * 2}]"
puts "42 is now a:     [kind $n]"

Each value starts as a pure string because it came from the script text. Reading its list length gives it a list form; arithmetic gives it an integer form. The string form is kept, so the change is invisible. Conversions happen at most once per form, so loops stay fast. The command is unsupported: its output is a debugging aid, not a stable interface.

$ tclsh shimmer.tcl
data starts as:  string
three elements:  3
data is now a:   list
42 starts as:    string
42 doubled:      84
42 is now a:     int

Conversions and canonical forms

expr parses a number and returns a canonical string: leading zeros disappear, a trailing fraction zero goes away, and a hexadecimal literal becomes decimal. The script converts several spellings and compares a string test with a numeric one.

convert.tcl
set values {007 3.10 {  42  } 1e3 0x10}

foreach v $values {
    puts [format "%-8s -> %s" [list $v] [expr {$v + 0}]]
}

puts "string equal:    [string equal 3.10 3.1]"
puts "numeric equal:   [expr {"3.10" == "3.1"}]"
puts "rounded:         [format %.2f 3.14159]"
puts "int(3.99):       [expr {int(3.99)}]"
puts "dict:            [dict create city {New York} age 28]"
puts "list:            [list {New York} 28]"

Adding zero forces a numeric reading: 007 becomes 7, 3.10 becomes 3.1, spaces vanish and 0x10 becomes 16. As characters 3.10 and 3.1 differ; as numbers they are equal. format %d refuses a fractional value, so rounding must be requested with %.2f or int().

$ tclsh convert.tcl
007      -> 7
3.10     -> 3.1
{  42  } -> 42
1e3      -> 1000.0
0x10     -> 16
string equal:    0
numeric equal:   1
rounded:         3.14
int(3.99):       3
dict:            city {New York} age 28
list:            {New York} 28

Empty string vs missing value

Three situations are easy to confuse: a variable never set, a variable set to the empty string, and a dictionary key present with an empty value. info exists, string length and dict exists tell them apart.

empty.tcl
set name ""
puts "exists:        [info exists name]"
puts "length:        [string length $name]"
puts "is empty:      [string equal $name ""]"

unset name
puts "still exists:  [info exists name]"

set cfg [dict create host {} port 8080]
puts "host key:      [dict exists $cfg host]"
puts "host value:    |[dict get $cfg host]|"
puts "timeout key:   [dict exists $cfg timeout]"
puts "integer check: [string is integer -strict [dict get $cfg host]]"

The empty variable exists with length zero; after unset it is gone and reading it would be an error. In the dictionary host is present with an empty value, so dict exists returns 1 and the value prints between two bars. timeout is absent, a different state, and the strict integer check rejects the empty value. Test with dict exists or info exists rather than comparing with "".

$ tclsh empty.tcl
exists:        1
length:        0
is empty:      1
still exists:  0
host key:      1
host value:    ||
timeout key:   0
integer check: 0

Source

The Tcl 8.6 manual is the authoritative reference for the value model and the commands used here. The string is classes are documented on the string page, and numeric syntax on the expr page.

This article covered the Tcl value model: strings read as numbers, booleans, lists and dictionaries, the checks that test those readings, and the internal forms cached behind them. Continue with Tcl string processing to see these values split, cleaned and formatted in real programs.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Tcl Tutorials.