Blast-Radius Aware AI Code Review for Business-Critical Systems
LiveReview LiveReview Explore LiveReview

classvariable - create link from local variable to variable in class

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::objdefinemethod 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
                      →Thisisinstance1of2
              set c [Counted new]
              $b report
                      →Thisisinstance2of3
              $c report
                      →Thisisinstance3of3

Keywords

       class, class variable, variable

TclOO                                                  0.3                                   classvariable(3tcl)

Name

       classvariable - create link from local variable to variable in class

See Also

global(3tcl), namespace(3tcl), oo::class(3tcl), oo::define(3tcl), upvar(3tcl), variable(3tcl)

Synopsis

       package require tcl::oo

       classvariablevariableName ?...?
________________________________________________________________________________________________________________

See Also