singleton(n)
π singleton(n) β a TclOO page, bestie edition
Okay so this is the manual page for singleton(n) from the TclOO Commands corner of the world. Sit down, hydrate, and let's get into it π
β¨ NAME β¨
oo::singleton β a class that does only allows one instance of itself π«Ά (yes, the original says it like that, and honestly? iconic phrasing, we're keeping it)
β¨ SYNOPSIS β¨
package require tcl::oo
oo::singleton method ?arg ...?
π CLASS HIERARCHY π
oo::object
β oo::class
β oo::singleton
Look at that lineage, the whole family tree is slaying π
π DESCRIPTION π
So. Singleton classes are classes that only permit at most one instance of themselves to exist. That's the whole vibe. One. Singular. Main character energy, no sequels.
Here's how they pull it off, no cap:
- They unexport the
createandcreateWithNamespacemethods entirely β like, gone, not invited to the group chat. - They override the
newmethod so that it only makes a new instance if there is no existing instance.
β οΈ Bestie, listen: it is not recommended to inherit from a singleton class β singleton-ness is not inherited. And it is not recommended that a singleton class's constructor take any arguments. Fr fr, just don't.
Instances get their destroy method overridden with a method that always returns an error, purely to discourage destruction of the object β but destruction remains possible if it's strictly necessary (e.g., by destroying the class, or by using rename to delete it). So like, the door's locked but the window's still open π
They also have a (non-exported) <cloned> method defined on them that similarly always returns errors, which makes attempts to use the singleton instance with oo::copy fail. Copy protection: enabled β¨
π CONSTRUCTOR
The oo::singleton class does not define an explicit constructor. Which means it's effectively the same as the constructor of the oo::class class. Nothing extra, nothing fancy β inherited, and honestly it ate π
π DESTRUCTOR
The oo::singleton class also does not define an explicit destructor. Destroying an instance of it is just like destroying an ordinary class (and yes β it will destroy the singleton object). π
π EXPORTED METHODS
cls new ?arg ...?β returns the current instance of the singleton class if one exists, and creates a new instance only if there's no existing instance. The extra arguments, arg ..., are only used if a new instance is actually manufactured β and that construction happens via theoo::classclass'snewmethod. This is an override of a superclass's method, with an identical call signature to the superclass's implementation. Same signature, different personality π
π NON-EXPORTED METHODS
The oo::singleton class explicitly states that create and createWithNamespace are unexported. So callers should not assume they have control over either the name or the namespace name of the singleton instance. It's giving "you don't get to name the baby" πΌ
π¦ EXAMPLE π¦
This example demonstrates that there is only one instance, even though the new method is called three times. Three calls, one object β the way that works is genuinely obsessed π₯
oo::singleton create Highlander {
method say {} {
puts "there can be only one"
}
}
set h1 [Highlander new]
set h2 [Highlander new]
if {$h1 eq $h2} {
puts "equal objects" β prints "equal objects"
}
set h3 [Highlander new]
if {$h1 eq $h3} {
puts "equal objects" β prints "equal objects"
}
πΈ Heads up: the name of the instance of the singleton is not guaranteed to be anything in particular. Don't count on it, bestie.
β SEE ALSO β
oo::class(n)
π KEYWORDS π
class, metaclass, object, single instance
Okay that's the whole page, go forth and singleton responsibly π§ββοΈ
This one's from TclOO, version 0.3, dated singleton(n) β mwah, love u, bye ππ
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- nixos 26.05: tcl 9.0.3
- Copyright (c) 2018 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms