class(n)
π class(n) β the manual page, but make it cute π
okay bestie, we're doing the class(n) page from the TclOO Commands section and honestly? it's giving main character energy. pull up a chair, grab a strawberry π, and let's get into it.
β¨ NAME β¨
oo::class β the class of all classes. yes literally. the mother of all mothers. the blueprint's blueprint. iconic behavior π
πΈ SYNOPSIS πΈ
package require tcl::oo
oo::class method ?arg ...?
so you load up the package and then you're just calling oo::class with a method and whatever arg ... you feel like throwing at it. that's the whole vibe check. π¦
π§ CLASS HIERARCHY π§
oo::object
β oo::class
oo::class is a subclass of oo::object, periodt. it sits right under it in the family tree and no, it's not going anywhere. π
π DESCRIPTION π
so. classes are objects that can manufacture other objects according to a pattern stored in the factory object β and that factory object is the class itself. that's the whole thing. a class is a little factory, and the blueprint it's running lives inside it. πβ¨
you make an instance of a class by calling one of the class's factory methods β typically either create, if you're the kind of girl who gives her objects an explicit name, or new, if you want a name picked automatically and you simply don't care to be involved. both valid. both slay.
now here's the mind-bending part, and I need you to sit down for it: oo::class is the class of all classes. every single class is an instance of this class β which means oo::class is an instance of itself. yes. it's its own mother. recursive queen. ππ
and because this class is a subclass of oo::object, every class is also an object. obviously. we contain multitudes.
you can define additional metaclasses (that's classes of classes, iykyk) if you genuinely need to, by subclassing oo::class. but heads up: the oo::class object hides the new method on itself, so new classes should always be made using the create method. don't come crying to me when new doesn't work on it, bestie, I warned you. π
π CONSTRUCTOR π
the constructor of the oo::class class takes an optional argument which β if it's actually present β gets sent to the oo::define command, along with the name of the newly-created class, so you can conveniently configure the class right at creation time. cute, efficient, on brand. π
π DESTRUCTOR π
the oo::class class does not define an explicit destructor. but here's the dramatic part, and it's lowkey terrifying: 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 not a breakup, it's a full timeline collapse. everything goes. no survivors. π₯
π EXPORTED METHODS π
these are the ones you're actually allowed to touch, bestie. go off.
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), passes the arguments arg ... to the constructor, and β if that returns a successful result β returns the fully qualified name of the created object. note that the result of the constructor itself is ignored, so don't bother returning anything cute from it. if the constructor fails (i.e. returns a non-OK result) then the object gets destroyed and the error message becomes the result of this method call. rip little object π«Άcls new ?arg ...?β creates a new instance of the class cls with a brand new unique name, passes the arguments arg ... to the constructor, and β if that returns a successful result β returns the fully qualified name of the created object (again, the constructor's result is ignored, sorry babe). 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.
β οΈ note that this method is not exported by the oo::class object itself, so classes should not be created using this method. read it again. I'll wait. π
π NON-EXPORTED METHODS π
the oo::class class supports the following non-exported methods β yes, they exist, no, they're not for you (unless they are, 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), passes the arguments arg ... to the constructor, and β if that returns a successful result β returns the fully qualified name of the created object (the constructor's result is ignored, as always). the name of the instance's internal namespace will be nsName, and 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. very "you get a custom namespace and you get a custom namespace" of it. β¨
π EXAMPLES π
okay this one's a whole saga and I'm obsessed with it. it defines a simple class hierarchy, makes a new instance of it, invokes a method of the object, and then destroys the hierarchy to show you that the destruction is transitive. watch. π
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"
so banana inherits from fruit, banana's eat peels itself first if it's not already edible, then calls next up the chain to fruit's eat, which says "yummy!". ππ and then fruit destroy nukes the whole bloodline and $b eat is suddenly just... an error. "unknown command". not me crying over a banana at 3am. π
π¦ SEE ALSO π¦
oo::define(n)oo::object(n)
go read those too, bestie, they're in the same universe. β¨
β KEYWORDS β
class, metaclass, object
and that's the page!! class, metaclass, object β the holy trinity, honestly.
this was TclOO, version 0.1, and the page is dated class(n) β so go forth, subclass responsibly, and remember: every class is an object, and oo::class is its own mother. periodt. π
ππ«Ά
π¦ where this page comes from
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- 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