3 ms·
The immutability and the lack of null are a big part of why Tcl is my favorite scripting language. I learned other languages at a similar level of abstraction
by networked 7y ago
The immutability and the lack of null are a big part of why Tcl is my favorite scripting language. I learned other languages at a similar level of abstraction before Tcl, but now when I program in Python or JavaScript their mutable collections feel like an obviously wrong default.
[upvar 1] and [uplevel 1], which, respectively, link a variable in your caller's stack frame to one in yours and run a command in the caller's stack frame, usually compose nicely. They are Tcl's replacement for macros and the lack of a compile time/run time distinction makes them, in a way, easier to reason about. However, [upvar N] and [uplevel N] aren't limited to N = 1, your immediate caller's stack frame. You can reach up an arbitrary number of stack frames. It allows you to design really complicated control structures. [upvar] links also nest. You can fall into a pattern of carrying around some sort of a context or configuration data with nested [upvar]s like a poor man's Reader monad. Both are difficult to debug when they go wrong and should be implemented with care and only when necessary.
Since version 8.5 Tcl has a built-in command for assigning list elements to variables:
lassign [procedure arg1 arg2] resultpart1 resultpart2
Conveniently, it also returns the remainder of the list if there were more list elements than variables. It leads to the idiom
set list [lassign $list foo bar baz]