local(3itcl)

πŸ“¦
version 4.3.8 Β· 2 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– local πŸ’– β€” itcl::local, aka "make me an object that ghosts the moment my proc dips"

okay bestie so we're in the [incr Tcl] manual, page local(3itcl), and this one is genuinely one of my faves because it's SO tidy. let's get into it 🫢


✨ NAME ✨

itcl::local β€” bestie, this creates an object that is local to a procedure. like, it lives and dies with the call frame. no cap.


✨ SYNOPSIS ✨

itcl::local className objName ?arg arg ...?

✨ DESCRIPTION ✨

so the local command creates an [incr Tcl] object that is local to the current call frame. and here's the iconic part: the second the call frame goes away, the object gets automatically deleted. no cleanup code, no delete object $x at the end of your proc, none of that. it just handles itself. πŸ’…

this is literally the whole point β€” it's useful for creating objects that are local to a procedure.

as a side effect, this command creates a variable named "itcl-local-xxx", where xxx is the name of the object that is created. and that variable is the secret sauce 🧚 β€” it detects when the call frame is destroyed and automatically deletes the associated object. it's giving "built-in safety net" and we're obsessed.


✨ EXAMPLE ✨

okay so in this example a simple "counter" object is used inside the procedure "test". the counter is created as a local object, so it is automatically deleted each time the procedure exits. and the puts statements in the constructor/destructor show the object coming and going as the procedure gets called β€” it's honestly so satisfying to watch, like a little cameo appearance 😭

itcl::class counter {
    private variable count 0
    constructor {} {
        puts "created: $this"
    }
    destructor {
        puts "deleted: $this"
    }

    method bump {{by 1}} {
        incr count $by
    }
    method get {} {
        return $count
    }
}

proc test {val} {
    local counter x
    for {set i 0} {$i < $val} {incr i} {
        x bump
    }
    return [x get]
}

set result [test 5]
puts "test: $result"

set result [test 10]
puts "test: $result"

puts "objects: [itcl::find objects *]"

✨ KEYWORDS ✨

class, object, procedure β€” the holy trinity, periodt πŸ‘‘


πŸ’€ quick vibe check reminder: that itcl-local-xxx variable isn't decoration, it's load-bearing. it's the thing that notices the call frame is gone and cleans up your object. don't go messing with it, bestie, or your object will just… linger. πŸ™ˆ

okay that's the whole page!! this one came from the itcl manual, page local(3itcl), and if you ever need to find it again, that's your reference right there. go forth and make local objects, you're gonna slay πŸŒˆβœ¨πŸ’–

πŸ“¦ where this page comes from