classvariable(n)

πŸ“¦
version 9.0.3 Β· 3 versions
, in alpine 3.23, alpine 3.24, nixos 26.05

πŸ’– NAME πŸ’–

okay bestie, welcome to the classvariable(n) page from the TclOO Commands manual πŸ’…. This one is literally classvariable β€” Create link from local variable to variable in class. No cap, that's the whole name.

✨ SYNOPSIS ✨

the group-chat summoning spell is:

package require tcl::oo

classvariable variableName ?...?

🌸 DESCRIPTION 🌸

okay so here's the tea β˜•:

  • The classvariable command is available within methods. That's its whole habitat.
  • It takes a series of one or more variable names and makes them available in the method's scope. Slay.
  • Those variable names must not be qualified and must not refer to array elements. Like, keep it simple, bestie.
  • 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 ✨. That's the main character energy right there.

Also, fun fact: this command is equivalent to the typevariable command provided by the snit package in tcllib for approximately the same purpose. Same vibe, different package πŸ’….

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. iykyk.

πŸ¦‹ EXAMPLE πŸ¦‹

This class counts how many instances of it have been made. It's giving "counter era" and honestly? Obsessed.

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

πŸŽ€ SEE ALSO πŸŽ€

go read these when you're done, bestie:

πŸ’… KEYWORDS πŸ’…

class, class variable, variable β€” that's the keyword trio, periodt.


πŸ’– TclOO 0.3 Β· classvariable(n) Β· no date listed, just vibes ✨

πŸ“¦ where this page comes from