local(n)

✨ local(n) β€” the itcl girlies' guide to local objects ✨

okay so bestie, this is the local page from itcl (that's [incr Tcl], the object-oriented glow-up for Tcl) πŸ’…


🌸 NAME 🌸

itcl::local β€” creates an object that is local to a procedure. that's it, that's the vibe 🫢


✨ SYNOPSIS ✨

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

πŸ’– DESCRIPTION πŸ’–

okay so the local command creates an [incr Tcl] object that is local to the current call frame. the moment that call frame dips out (goes away), the object gets automatically deleted β€” no cleanup code, no tears, no mess 😭✨

it's giving extremely useful for making objects that are local to a procedure. like, you want a lil guy that lives and dies with your proc? this is the command. periodt.

🌟 as a side effect, this command creates a variable named "itcl-local-xxx", where xxx is the name of the object that is created. this variable is the one keeping watch πŸ‘€ β€” it detects when the call frame is destroyed and automatically deletes the associated object. so like, don't be weird about that variable, it's the object's emotional support variable fr fr.

πŸŽ€ EXAMPLE πŸŽ€

in the following example, a simple "counter" object is used within the procedure "test". the counter is created as a local object, so it is automatically deleted each time the procedure exits. the puts statements in the constructor/destructor show the object coming and going as the procedure is called β€” main character entrances and exits only πŸ‘‘

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

and that's the whole page bestie!! πŸ“ this came from the itcl source, it's the local(n) manual page β€” no date on it, so we're just crediting the itcl era it lives in πŸ’œ go make some objects that know when to leave the party 🧚