class(3tcl)

✨ class(3tcl) ✨

okay bestie, welcome to class from the TclOO Commands manual β€” this is the page for oo::class, aka the class of all classes. yes, it's giving main character energy. let's get into it 🫢


πŸ’– NAME πŸ’–

oo::class β€” the class of all classes. periodt.


🌸 SYNOPSIS 🌸

package require tcl::oo

oo::class method ?arg ...?

πŸ¦‹ CLASS HIERARCHY πŸ¦‹

oo::object
   β†’ oo::class

like, oo::class is literally a subclass of oo::object. the family tree is small but iconic.


πŸ‘‘ DESCRIPTION πŸ‘‘

so here's the tea β˜•: classes are objects that can manufacture other objects according to a pattern stored in the factory object (the class). to make an instance of the class, you call one of the class's factory methods β€” usually either create if you're giving it an explicit name, or new if you want a unique name to be picked automatically.

oo::class is the class of all classes. every single class is an instance of this class, which means... yeah, it's an instance of itself. 😭 recursive queen behavior. this class is a subclass of oo::object, so every class is also an object. additional metaclasses (i.e., classes of classes) can be defined if you need them, by subclassing oo::class.

⚠️ heads up bestie: the oo::class object hides the new method on itself, so new classes should always be made using the create method. no shortcuts, sorry. πŸ’…

πŸŽ€ CONSTRUCTOR πŸŽ€

the constructor of the oo::class class takes an optional argument which, if present, is sent to the oo::define command (along with the name of the newly-created class) so you can conveniently configure the class at creation time. cute, right? ✨

πŸ’€ DESTRUCTOR πŸ’€

the oo::class class does not define an explicit destructor. however β€” and this is the dramatic part β€” when a class is destroyed, all its subclasses and instances are also destroyed, along with all objects that it has been mixed into. it's giving transitive destruction, and honestly? terrifying. πŸ™ˆ

🌈 EXPORTED METHODS 🌈

  • cls create name ?arg ...? β€” creates a new instance of the class cls called name (which gets resolved within the calling context's namespace if it's not fully qualified), passing the arguments arg ... to the constructor, and (if that returns a successful result) returning the fully qualified name of the created object β€” note that the result of the constructor itself is ignored. if the constructor fails (i.e. returns a non-OK result) then the object gets destroyed and the error message is the result of this method call. no half-baked objects here bestie 🫢
  • cls new ?arg ...? β€” creates a new instance of the class cls with a new unique name, passing the arguments arg ... to the constructor, and (if that returns a successful result) returning the fully qualified name of the created object (again, the result of the constructor is ignored). if the constructor fails (i.e., returns a non-OK result) then the object is destroyed and the error message is the result of this method call.
🚨 but wait β€” this method is not exported by the oo::class object itself, so classes should not be created using this method. i said what i said.

🧚 NON-EXPORTED METHODS 🧚

the oo::class class supports these non-exported methods too (iykyk):

  • cls createWithNamespace name nsName ?arg ...? β€” creates a new instance of the class cls called name (resolved within the calling context's namespace if it's not fully qualified), passing the arguments arg ... to the constructor, and (if that returns a successful result) returning the fully qualified name of the created object (the result of the constructor is ignored, as always πŸ™ƒ). the name of the instance's internal namespace will be nsName; it is an error if that namespace cannot be created. if the constructor fails (i.e., returns a non-OK result) then the object is destroyed and the error message is the result of this method call.

πŸ“ EXAMPLES πŸ“

this example defines a simple class hierarchy and creates a new instance of it. it then invokes a method of the object before destroying the hierarchy and showing that the destruction is transitive. watch and learn πŸ’…

oo::class create fruit {
    method eat {} {
        puts "yummy!"
    }
}
oo::class create banana {
    superclass fruit
    constructor {} {
        my variable peeled
        set peeled 0
    }
    method peel {} {
        my variable peeled
        set peeled 1
        puts "skin now off"
    }
    method edible? {} {
        my variable peeled
        return $peeled
    }
    method eat {} {
        if {![my edible?]} {
            my peel
        }
        next
    }
}
set b [banana new]
$b eat               β†’ prints "skin now off" and "yummy!"
fruit destroy
$b eat               β†’ error "unknown command"

like, fruit destroy takes the whole banana down with it. transitive destruction ate and left no crumbs πŸŒπŸ’€


⭐ SEE ALSO ⭐

  • oo::define(3tcl)
  • oo::object(3tcl)

πŸ’œ KEYWORDS πŸ’œ

class, metaclass, object


and that's the whole page bestie!! this was the TclOO manual, version 0.1, dated back in the day, and it lives at class(3tcl) β€” go forth and subclass responsibly πŸŽ€βœ¨

πŸ“¦ where this page comes from