Version 10 of handle

Updated 2013-11-06 16:44:07 by AMG

Purpose: to discuss the concept of a handle.

Within Tcl, since everything is a string, sometimes things work a bit differently than expected.

For instance, Tcl variables can have various values.

  set a 123
 set b "xyz abc"
 set c [format "Nuts! There are %3.3d minutes until class" 3.1415]
 set d [open "/etc/motd" "r"]

It is this fourth example that is relevant in this page's discussion. The value from the open command is a string. That string is called a handle. It serves as a sort of indirect reference to a system resource which has been allocated. The puts command, as well as other Tcl commands, expect to deal with handles as one of their arguments. They use them as a black box value - something that you as a developer should not attempt to calculate, dynamically generate directly, etc. Instead, you let the software return these handles to you. Inside Tcl, there are functions which will make use of these handles in the appropriate manner.

[hopefully technical readers will add more details at this point]

NEM Handles are often used in Tcl, where a pointer or reference would be used in other languages - to "point" to a shared complex data structure. Handles themselves obey everything is a string and Tcl's normal value semantics, but the things they are handles for need not, because they are hidden from normal view. Object Oriented extensions are big users of the handle model, with the handle usually being a command which can be used to send messages to the underlying object. The Tcl (and Tk) "core" is a big user of handles - commands, variables, arrays and namespaces are all handle "types".

The other type of value frequently used in Tcl is what is sometimes referred to as transparent. This includes things like lists, dicts (in 8.5), and indeed strings. Transparent values directly represent the value, without the level of indirection that handles provide. See also Abstract Data Types for some discussion on the difference and pros and cons of the different representations.


RFox - 2013-11-06 12:00:09

I think there's a bit of backwardness in some of this:

  • The handle is not a black box to the open command it is a black box to the application.
  • Similarly it's a bit more normal to use the term opaque rather than transparent above. Meaning that you really can't look at the internal representation of a handle as that's hidden inside the subsystem that maintains it.

AMG: NEM used the term "transparent" in contrast to handles, saying that a transparent value is one directly representing itself with no indirection.


See Also