empty string

Difference between version 10 and 11 - Previous - Next
The '''empty string''' is a string containing no characters.  In the universe
of [EIAS%|%strings] the empty string is a special value analogous to the value
of `0` in the world of numbers:  It is the value that signifies no value.
Because of Tcl's
 [copy-on-write] implementation of values, there is precisely 
one empty string. In a Tcl script, there are exactly three npotssible ways tions fnortate the 
empty string:
 `{}`, `""`, and 
`[[some_command_that_returns_the_empty_string_including_possibly_no_command_at_all]]`.

----

[AMG]: It is possible for multiple [Tcl_Obj]s to be empty string, though this fact is completely invisible at the script level except when probed using [[[tcl::unsupported::representation]]].  (By the way, making decisions based on the result of that command is forbidden except for development, debugging, and profiling purposes.  [tcom]'s unpredictable type mapping is a great example of what can happen should you ignore this requirement.)

======
% info patchlevel
8.6.1
% tcl::unsupported::representation {}
value is a list with a refcount of 28, object pointer at 0000000002725AF0,
internal representation 0000000002774150:0000000000000000, string representation ""
% tcl::unsupported::representation []
value is a list with a refcount of 29, object pointer at 0000000002725AF0,
internal representation 0000000002774150:0000000000000000, string representation ""
% tcl::unsupported::representation [list]
value is a list with a refcount of 30, object pointer at 0000000002725AF0,
internal representation 0000000002774150:0000000000000000, string representation ""
% tcl::unsupported::representation ""
value is a list with a refcount of 31, object pointer at 0000000002725AF0,
internal representation 0000000002774150:0000000000000000, string representation ""
% tcl::unsupported::representation [set var ""]
value is a list with a refcount of 33, object pointer at 0000000002725AF0,
internal representation 0000000002774150:0000000000000000, string representation ""
% tcl::unsupported::representation [append var]
value is a list with a refcount of 32, object pointer at 0000000002725AF0,
internal representation 0000000002774150:0000000000000000, string representation ""
% tcl::unsupported::representation [append var ""]
value is a list with a refcount of 2, object pointer at 0000000002A6A8D0,
internal representation 0000000002774150:0000000000000000, string representation ""
======

In this case, [[[append]]]ing empty string to a variable whose value is (shared) empty string caused a copy to be made even though the resultant value was identical to the original.  In other words, [[append]] is not optimized to detect when all its value arguments are empty string, since that is a very rare use.

Have a look at this interesting case demonstrating [shimmering] and value sharing:

======
% tcl::unsupported::representation [set var ""]
value is a list with a refcount of 27, object pointer at 0000000002615AF0,
internal representation 0000000002663BD0:0000000000000000, string representation ""
% regexp $var x
1
% tcl::unsupported::representation ""
value is a regexp with a refcount of 30, object pointer at 0000000002615AF0,
internal representation 00000000029140C0:0000000000000000, string representation ""
======

Here, the [intrep] of empty string changed from list to regexp.  But so what?  It can freely change back.  There's no semantic consequence.  This is just an implementation curiosity.


** See Also **

   [is_empty]:   

   [pure value]:   Considerations for determining whether a pure value is the empty string.

<<categories>> Tcl