object(n)

πŸ¦‹ object(n) β€” the TclOO root class, bestie edition πŸ¦‹

okay so you're here about oo::object, from the TclOO Commands manual (that's the n in object(n), iykyk). it's literally the moment. let's go πŸ’…


✨ NAME ✨

oo::object β€” the root class of the class hierarchy. the mother of all classes fr fr πŸ‘‘


✨ SYNOPSIS ✨

package require tcl::oo

oo::object method ?arg ...?

🌸 CLASS HIERARCHY 🌸

oo::object

that's it. that's the whole tree. she's at the top, no parents, main character energy πŸ’–


πŸ’œ DESCRIPTION πŸ’œ

so. the oo::object class is the root class of the object hierarchy β€” every single object out here is an instance of this class. and because classes are themselves objects (yes, they are, slay), classes are instances of this class too. objects are always referred to by their name, and they can be renamed while maintaining their identity β€” glow-up without losing yourself, we love that πŸ¦‹

you can make instances of objects with either the create or new methods of the oo::object object itself, or by invoking those methods on any of the subclass objects. want the tea on that? go see oo::class. and if you want to configure individual objects β€” like instance-specific methods, mixed-in classes, etc. β€” that's the oo::objdefine command's job πŸŽ€

every object has a unique namespace attached to it: the instance namespace. this namespace holds all the instance variables of the object, and it will be the current namespace whenever a method of the object is invoked (including a method of the class of the object). when the object is destroyed, its instance namespace gets deleted β€” no lingering, we clean up after ourselves ✨

the instance namespace also contains the object's my command, which you can use to invoke non-exported methods of the object, or to create a reference to the object for the purpose of invocation that persists across renamings of the object. iconic behaviour honestly 🫢


πŸ”¨ CONSTRUCTOR πŸ”¨

the oo::object class does not define an explicit constructor. lowkey mysterious of her πŸ’€


πŸ’€ DESTRUCTOR πŸ’€

the oo::object class does not define an explicit destructor either. she simply does not do goodbyes 😭


πŸ‘‘ EXPORTED METHODS πŸ‘‘

the oo::object class supports the following exported methods:

  • obj destroy β€” this method destroys the object, obj, that it's invoked upon, invoking any destructors on the object's class in the process. it's equivalent to using rename to delete the object command. the result of this method is always the empty string. bye bestie, no cap πŸŽ€

πŸ™ˆ NON-EXPORTED METHODS πŸ™ˆ

the oo::object class supports the following non-exported methods:

  • obj eval ?arg ...? β€” this method concatenates the arguments, arg, as if with concat, and then evaluates the resulting script in the namespace that is uniquely associated with obj, returning the result of the evaluation.

note that object-internal commands such as my and self can be invoked in this context. very insider-info, very iykyk πŸ’…

  • obj unknown ?methodName? ?arg ...? β€” this method is called when an attempt to invoke the method methodName on object obj fails. the arguments that the user supplied to the method are given as arg arguments. if methodName is absent, the object was invoked with no method name at all (or any other arguments). the default implementation (i.e., the one defined by the oo::object class) generates a suitable error, detailing what methods the object supports given whether the object was invoked by its public name or through the my command. so it's the "who?? never heard of her" bouncer 🚫
  • obj variable ?varName ...? β€” this method arranges for each variable called varName to be linked from the object obj's unique namespace into the caller's context. thus, if it's invoked from inside a procedure then the namespace variable in the object is linked to the local variable in the procedure. each varName argument must not have any namespace separators in it. the result is the empty string. keep it simple, keep it local πŸ’–
  • obj varname varName β€” this method returns the globally qualified name of the variable varName in the unique namespace for the object obj. giving you the full address, no shortcuts 🌈
  • obj <cloned> sourceObjectName β€” this method is used by the oo::object command to copy the state of one object to another. it is responsible for copying the procedures and variables of the namespace of the source object (sourceObjectName) to the current object. it does not copy any other types of commands or any traces on the variables; that can be added if desired by overriding this method in a subclass. so: partial copy only, don't come for her about the traces πŸ’…

⭐ EXAMPLES ⭐

this example demonstrates basic use of an object. it's giving tutorial, it's giving growth 🌱

set obj [oo::object new]
$obj foo             β†’ error "unknown method foo"
oo::objdefine $obj method foo {} {
    my variable count
    puts "bar[incr count]"
}
$obj foo             β†’ prints "bar1"
$obj foo             β†’ prints "bar2"
$obj variable count  β†’ error "unknown method variable"
$obj destroy
$obj foo             β†’ error "unknown command obj"

look at her go. bar1, bar2, gone. periodt πŸ”₯


πŸ¦‹ SEE ALSO πŸ¦‹

my(n), oo::class(n)

go read those next, bestie, they're connected πŸ‘‘


πŸŽ€ KEYWORDS πŸŽ€

base class, class, object, root class


ok real talk for a sec: this whole page comes from the TclOO package docs, version 0.1, and yeah that's genuinely the version β€” baby numbers, but she's the root class so she's been holding it down forever πŸ’–

stay classy, stay rooted, and remember: every object you ever make is an instance of her. no cap. mwah πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from