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: theoo::classobject hides thenewmethod on itself, so new classes should always be made using thecreatemethod. 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 classclscalled 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 classclswith 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 classclscalled 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
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2007 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms