RosettaCodeData/Task/Enforced-immutability/Tcl/enforced-immutability-1.tcl
2023-07-01 13:44:08 -04:00

10 lines
428 B
Tcl

proc constant {varName {value ""}} {
upvar 1 $varName var
# Allow application of immutability to an existing variable, e.g., a procedure argument
if {[llength [info frame 0]] == 2} {set value $var} else {set var $value}
trace add variable var write [list apply {{val v1 v2 op} {
upvar 1 $v1 var
set var $val; # Restore to what it should be
return -code error "immutable"
}} $value]
}