Artifact b7634e9f1b4d704b0fd58cc0f79562bac814563c5f01fc93a301e59d32817f79:
- File doc/markdown/classvariable.md — part of check-in [36f9a689ca] at 2026-01-17 15:52:32 on branch tip-700 — - made sure that nested Dlist items will be indented properly in the markdown output - made sure that new paragraphs inside Dlist items are divided by a newline - do all bachslah substitutions also in Code blocks - unify binary.n (user: Torsten, size: 1996) [annotate] [blame] [check-ins using]
CommandName: classvariable ManualSection: n Version: 0.3 TclPart: TclOO TclDescription: TclOO Commands Links:
- global(n)
- namespace(n)
- oo::class(n)
- oo::define(n)
- upvar(n)
- variable(n) Keywords:
- class
- class variable
- variable Copyright:
- Copyright (c) 2011-2015 Andreas Kupries
- Copyright (c) 2018 Donal K. Fellows ---
Name
classvariable - Create link from local variable to variable in class
Synopsis
::: {.synopsis} ::: [package]{.cmd} [require]{.sub} [tcl::oo]{.lit}
[classvariable]{.cmd} [variableName]{.arg} [variableName]{.optdot} :::
Description
The classvariable command is available within methods. It takes a series of one or more variable names and makes them available in the method's scope; those variable names must not be qualified and must not refer to array elements. The originating scope for the variables is the namespace of the class that the method was defined by. In other words, the referenced variables are shared between all instances of that class.
Note that this command is equivalent to the command typevariable provided by the snit package in tcllib for approximately the same purpose. If used in a method defined directly on a class instance (e.g., through the oo::objdefine method definition) this is very much like just using:
namespace upvar [namespace current] $var $var
for each variable listed to classvariable.
Example
This class counts how many instances of it have been made.
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