classvariable(3tcl)
π classvariable β the TclOO glow-up for class-wide variables π
okay bestie, so this is classvariable from the TclOO manual (that's the classvariable(3tcl) page, section 3tcl, TclOO Commands β you're in the right place, don't worry π«Ά).
β¨ NAME β¨
classvariable β Create link from local variable to variable in class
πΈ SYNOPSIS πΈ
package require tcl::oo
classvariable variableName ?...?
π¦ DESCRIPTION π¦
so. the classvariable command. it's only available inside methods β like, it lives in method scope, that's its whole thing, no cap. π
you hand it a series of one or more variable names, and it makes those names available in the method's scope. super easy.
but here's the rules, and they matter:
- those variable names must not be qualified β no dotted paths, no namespaces, none of that π
- and they must not refer to array elements β so no
foo(bar)situation
and where do the variables actually live? the originating scope is the namespace of the class that the method was defined by. so basically: the variables you're linking are shared between all instances of that class. group chat for variables. iconic. β¨
π quick note fr fr: this command is equivalent to thetypevariablecommand provided by thesnitpackage intcllibβ they're for approximately the same purpose. so if you've metsnitbefore, you already know the vibe.
and one more lil thing: if you use it in a method defined directly on a class instance (like, through the oo::objdefine method definition), then classvariable is very much like just using this:
namespace upvar [namespace current] $var $var
β¦for each variable listed to classvariable. like, one of those per variable. yes, really. π
π EXAMPLE π
this class counts how many instances of it have been made. counting era, we love her π
oo::class create Counted {
initialise {
variable count 0
}
variable number
constructor {} {
classvariable count
set number [incr count]
}
method report {} {
classvariable count
puts "This is instance $number of $count"
}
}
set a [Counted new]
set b [Counted new]
$a report
β This is instance 1 of 2
set c [Counted new]
$b report
β This is instance 2 of 3
$c report
β This is instance 3 of 3
not the count going 1, 2, 3 and every instance knowing about it π the shared class variable is doing ALL the work and it's serving fr fr.
π SEE ALSO π
global(3tcl)namespace(3tcl)oo::class(3tcl)oo::define(3tcl)upvar(3tcl)variable(3tcl)
π KEYWORDS π
class, class variable, variable
and that's the whole page, bestie!! go forth and share your variables across every instance, you absolute main character π§β
mwah π β this was the TclOO manual, version 0.3, page classvariable(3tcl). periodt. π
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2011-2015 Andreas Kupries
- Copyright (c) 2018 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms