nx::Object(3nx)
β¨ NAME β¨
This is the manual page for nx::Object (3nx) β API reference of the base class in the NX object system. periodt π
π¦ SYNOPSIS π¦
nx::Object create obj ?-object-mixins mixinSpec? ?-class newClassName?
?-object-filters filterSpec? ?initBlock?
nx::Object new ?-object-mixins mixinSpec? ?-class newClassName?
?-object-filters filterSpec? ?initBlock?
obj ?public | private | protected? object alias ?-debug? ?-deprecated?
methodName ?-returns valueChecker? ?-frame object | method? cmdName
obj cget configurationOption
obj configure ?configurationOption value ...?
obj contains ?-withnew trueFalse? ?-object objectName? ?-class
className? cmds
obj copy ?newObjectName?
obj delete object feature arg
obj destroy
obj eval arg ?arg ...?
obj object filters submethod ?arg ...?
obj ?public | protected | private? object forward ?-debug? ?-deprecated?
methodName ?-prefix prefixName? ?-frame object? ?-returns valueChecker?
?-verbose? ?target? ?arg ...?
obj info baseclass
obj info children ?-type className? ?pattern?
obj info class
obj info has ?mixin | namespace | type? ?arg ...?
obj info lookup submethod ?arg ...?
obj info name
obj info info ?-asList?
obj info object filters ?-guards? ?pattern?
obj info object method option methodName
obj info object methods ?-callprotection level? ?-type methodType?
?-path? ?namePattern?
obj info object mixins ?-guards? ?pattern?
obj info object slots ?-type className? ?pattern?
obj info object variables ?pattern?
obj info parent
obj info precedence ?-intrinsic? ?pattern?
obj info variable option handle
obj info vars ?pattern?
obj ?public | protected | private? object method ?-debug? ?-deprecated?
name parameters ?-checkalways? ?-returns valueChecker? body
obj move newObjectName
obj object mixins submethod ?arg ...?
obj object property ?-accessor public | protected | private? ?-class
className? ?-configurable trueFalse? ?-incremental? ?-nocomplain?
?-trace set | get | default? spec ?initBlock?
obj require namespace
obj require ?public | protected | private? object method methodName
obj unknown unknownMethodName ?arg ...?
obj uplevel ?level? arg1 ?arg2 ...?
obj upvar ?level? otherVar1 localVar1 ?otherVar2 localVar2 ...?
obj object variable ?-accessor public | protected | private?
?-incremental? ?-class className? ?-configurable trueFalse? ?-initblock
script? ?-trace set | get | default? ?-nocomplain? spec ?defaultValue?
π DESCRIPTION π
okay so nx::Object is literally the base class of the NX object system, bestie. Every single object defined in NX is a (direct or indirect) instance of this base class. The methods provided by the nx::Object base class are available to all objects and to all classes defined in NX. no cap. π
+---------+
| ::nx::* |
+---------+--------------------------------------Y
| |
| +---------+ instance of +----------+ |
| | |<....................| | |
| | Class | | Object | |
| | |....................>| | |
| +----+----+ subclass of +-----+----+ |
| ^ ^ ^ |
instance.|...........................|....|......./
of | | |
+-----+-----+ subclass of | | instance
| |.....................| | of
| /cls/ | (by default) |
| | |
+-----------+ |
^ |
instance |.............(xor)..............|
of | +-----------+ |
|.........| |..........|
| /obj/ |
| |
+-----------+
NX allows for creating and for using objects (e.g. obj) which are instantiated from the base class nx::Object directly. Typical use cases are singletons and anonymous, inline objects. In such use cases, NX does not require creating an intermediate application class (e.g. cls), which specializes the base class nx::Object by default, beforehand. lowkey convenient. β¨
Objects (e.g. obj) which are created by instantiating a previously defined application class (e.g. cls) are indirect instances of nx::Object. π¦
Direct instances of nx::Object can be created as follows:
nx::Object create obj ?-object-mixins mixinSpec? ?-class newClassName?
?-object-filters filterSpec? ?initBlock?
To create a direct instance of nx::Object having an explicit name obj, use create on nx::Object. Note that create is defined by nx::Class and is available to nx::Object being an instance of nx::Class. This way, singleton objects can be created, for example. π
nx::Object new ?-object-mixins mixinSpec? ?-class newClassName?
?-object-filters filterSpec? ?initBlock?
To create a direct instance of nx::Object having an automatically assigned, implicit object name, use new on nx::Object. Note that new is defined by nx::Class and is available to nx::Object being an instance of nx::Class. Using new allows for creating anonymous, inline objects, for example. π
The configuration options for direct and indirect instances of nx::Object, which can be passed when calling create and new, are documented in the subsequent section. keep scrolling bestie. π
πΈ CONFIGURATION OPTIONS FOR INSTANCES OF NX::OBJECT πΈ
Configuration options can be used for configuring objects during their creation by passing the options as non-positional arguments into calls of new and create (see nx::Class). An existing object can be queried for its current configuration using cget and it can be re-configured using configure. Legal configuration options are:
-class ?className?β Retrieves the current class of the object or sets the object's class toclassName, if provided. π-object-filters ?filterMethods?β Retrieves the list of currently active per-object filter methods or sets a list of per-object filter methods, iffilterMethodsis provided. β¨-object-mixins ?mixinSpecs?β IfmixinSpecsis not specified, retrieves the list of currently active per-object mixin specifications. IfmixinSpecsis specified, sets a list of per-object mixin specifications to become active. mixin classes are returned or set in terms of a list of mixin specifications. π
π METHODS FOR INSTANCES OF NX::OBJECT π
alias π
obj ?public | private | protected? object alias ?-debug? ?-deprecated? methodName ?-returns valueChecker? ?-frame object | method? cmdName
Define an alias method for the given object. The resulting method registers a pre-existing Tcl command cmdName under the (alias) name methodName with the object. If cmdName refers to another method, the corresponding argument should be a valid method handle. If a Tcl command (e.g., a proc), the argument should be a fully qualified Tcl command name. If aliasing a subcommand (e.g., array exists) of a Tcl namespace ensemble (e.g., array), cmdName must hold the fully qualified subcommand name (and not the ensemble name of the subcommand). no cap. π
As for a regular object method, -returns allows for setting a value checker on the values returned by the aliased command cmdName. β¨
When creating an alias method for a C-implemented Tcl command (i.e., command defined using the Tcl/NX C-API), -frame sets the scope for variable references used in the aliased command. If the provided value is object, then variable references will be resolved in the context of the called object, i.e., the object upon which the alias method is invoked, as if they were object variables. There is no need for using the colon-prefix notation for identifying object variables. If the value is method, then the aliased command will be executed as a regular method call. The command is aware of its called-object context; i.e., it can resolve ::nx::self. In addition, the alias method has access to the method-call context (e.g., nx::next). If -frame is omitted, and by default, the variable references will resolve in the context of the caller of the alias method. π«Ά
To express deprecation of the alias method methodName, set the -deprecated flag. Deprecated methods remain usable from client code, but their usage will be signaled to the developer and/or can be tracked using ::nsf::deprecated. To register methodName with the debugger, set the -debug flag. Entering and exiting a method, which was flagged for debugging, is recorded by calling the redefinable callback procs ::nsf::debug::call and ::nsf::debug::exit, respectively. By default, these callbacks forward to ::nsf::log, which can also be customized at the script level. π
ngl, deprecated methods are giving βIβm still here but Iβm on thin iceβ energy. π
cget π
obj cget configurationOption
The method is used to obtain the current value of configurationOption for obj. The configuration options available for querying through cget are determined by the configurable properties defined by the class hierarchy of obj. The queryable configuration options for obj can be obtained by calling info lookup syntax configure. The configurationOption can be set and modified using configure. β¨
% nx::Object create obj
::obj
% ::obj info lookup syntax configure
?-object-mixins /mixinreg .../? ?-class /class/? ?-object-filters /filterreg .../? ?/__initblock/?
% ::obj cget -class
::nx::Object
configure
π οΈ obj configure ?configurationOption value ...?
Okay bestie, this one sets configuration options on an object. Which options can you set on obj? That's decided by the configurable properties defined by obj's class hierarchy. Wanna know what's actually settable? Call info lookup syntax configure and it'll tell you. π
Also, plot twist: configure is also called during object construction β it receives the arguments passed into calls of create and new. And anything you set with configure? Grab it back with cget. Iconic.
% nx::Class create Foo {:property x}
::Foo
% Foo create f1 -x 101
::f1
% f1 cget -x
101
% f1 configure -x 200
% f1 cget -x
200
πͺ contains
obj contains ?-withnew trueFalse? ?-object objectName? ?-class className? cmds
This is the builder for nested object structures, fr fr. The object and class construction statements you pass as the last argument cmds get evaluated so that the receiver object obj becomes the parent of the newly constructed objects and classes. How? By explicitly setting the namespace for constructing relatively named objects. Fully qualified object names in cmds dodge the nesting entirely β they stay where they are. π¦
-withnewβ requests automatic rescoping of objects created withnew, so they become nested into the receiver objectobjinstead of being created in the default namespace for autonamed objects (that's::nsf). Turn it off and autonamed objects do not become children ofobj.
You can pick a different parent object than obj using -object objectName. If that explicitly set parent object doesn't exist before you call contains, it gets created on the fly as a direct instance of nx::Object. Or use -class className to hand over a class other than nx::Object for that on-the-fly creation of objectName.
% nx::Class create Window {
:contains {
#
# Become children of Window, implicitly
#
nx::Class create Header; # Window::Header
nx::Object create Panel; # Window::Panel
}
#
# Explicitly declared a child of Window using [self]
#
nx::Class create [self]::Slider; # Window::Slider
#
# Fully-qualified objects do not become nested
#
nx::Class create ::Door; # ::Door
}
::Window
% ::Window info children
::Window::Panel ::Window::Header ::Window::Slider
πͺ copy
obj copy ?newObjectName?
Makes a full and deep copy of a source object obj. The copy gets every structural and behavioral property of the source β object variables, per-object methods, nested objects, slot objects, namespaces, filters, mixins, and traces. All of it. β¨
You can name the copy explicitly by passing newObjectName, or it just gets named automatically (in the spirit of new of nx::Class).
ποΈ delete
obj delete object feature arg
This is the equivalent to Tcl's rename, except it's for removing structural features (properties, variables) and behavioral features (methods) off the object:
obj delete object property propertyName
obj delete object variable variableName
obj delete object method methodName
Respectively: removes a property propertyName, a variable variableName, and a method methodName that were previously defined for the scope of the object.
And heads up β delete object method works just as well for removing regular methods (see object method), an alias method (see object alias), and a forwarder method (see object forward). It does not discriminate. π
π₯ destroy
obj destroy
This lets you explicitly destruct an object obj, potentially before obj gets destroyed by the object system (like, say, during shutdown of the object system when you call exit):
[nx::Object new] destroy
If you provide a custom implementation of destroy, you can customize the destruction procedure of obj. Typically, once your application-specific destruction logic is done, a custom destroy triggers the actual, physical object destruction via next. π«Ά
% [nx::Object create obj {
:public method destroy {} {
puts "destroying [self]"
next; # physical destruction
}
}] destroy
destroying ::obj
You can also make a customized object-destruction scheme shared between the instances of a class by defining the custom destroy for an application class:
% nx::Class create Foo {
:method destroy {} {
puts "destroying [self]"
next; # physical destruction
}
}
::Foo
% Foo create f1
::f1
% f1 destroy
destroying ::f1
Physical destruction happens by clearing the in-memory object storage of obj. That's done by passing obj into a call to dealloc provided by nx::Class. A near, scripted equivalent to the C-implemented destroy provided by nx::Object would look like this:
% Object method destroy {} {
[:info class] dealloc [self]
}
β οΈ But note, bestie: destroy is protected against application-level redefinition. Try to evaluate that script snippet above and you get:
refuse to overwrite protected method 'destroy'; derive e.g. a subclass!
A custom destroy must be provided as a refinement in a subclass of nx::Object or in a mixin class. No shortcuts. π
π§ͺ eval
obj eval arg ?arg ...?
Evaluates a special Tcl script for the scope of obj, in the style of Tcl's eval. But there are notable differences from the standard eval: in this script, the colon-prefix notation is available to dispatch to methods and to access variables of obj. And you can define script-local variables β they store intermediate results and get thrown away once the evaluation of the script completes. πΈ
% nx::Object create obj {
:object property {bar 1}
:public object method foo {x} { return $x }
}
::obj
% ::obj eval {
set y [:foo ${:bar}]
}
1
ποΈ filters
obj object filters submethod ?arg ...?
Accesses and modifies the list of methods registered as filters with obj, using a specific setter or getter submethod. Let's go through them all, no skipping. π
obj object filters add spec ?index?
- Inserts a single filter into the current list of filters of
obj. Usingindex, you can set a position in the existing list of filters for inserting the new filter. If you omit it,indexdefaults to the list head (0).
obj object filters clear
- Removes all filters from
objand returns the list of removed filters. Clearing is equivalent to passing an empty list forfilterSpecListtoobject filter set.
obj object filters delete ?-nocomplain? specPattern
- Removes a single filter from the current list of filters of
objwhose spec matchesspecPattern.specPatterncan contain special matching chars (seestring match).object filters deletewill throw an error if there's no matching filter β unless-nocomplainis set.
obj object filters get
- Returns the list of current filter specifications registered for
obj.
obj object filters guard methodName ?expr?
- If
expris specified, registers a guard expressionexprwith a filtermethodName. This requires that the filtermethodNamehas been previously set usingobject filters setor added usingobject filters add.exprmust be a valid Tcl expression (seeexpr). An empty string forexprwill clear the currently registered guard expression for filtermethodName. - If
expris omitted, returns the guard expression set on the filtermethodNamedefined forobj. If none is available, an empty string gets returned.
obj object filters methods ?pattern?
- If
patternis omitted, returns all filter names which are defined byobj. By specifyingpattern, the returned filters can be limited to those whose names matchpatterns(seestring match).
So like, obj object filters set filterSpecList β filterSpecList takes a list of filter specs, and every single spec is itself either a one-element or a two-element list: methodName ?-guard guardExpr?. The methodName there points at an existing method of obj, and that method becomes registered as a filter. And if a spec has three elements instead, that third element guardExpr gets stored as a guard expression of the filter. That guard expression must be a valid Tcl expression (peep expr for that π
). expr is evaluated whenever obj receives a message, to figure out whether the filter should intercept that message. So basically guard expressions are how you realize context-dependent or conditional filter composition. Slay.
Every methodName in a spec has to resolve to an existing method in the scope of the object. And if you wanna access or manipulate the list of filters of obj, cget|configure -object-filters works too. π«Ά
π forward π
obj ?public | protected | private? object forward ?-debug?
?-deprecated? methodName ?-prefix prefixName? ?-frame object?
?-returns valueChecker? ?-verbose? ?target? ?arg ...?
Okay so this defines a forward method for the given object. What even happens: the definition of a forward method registers a predefined-but-changeable list of forwarder arguments under the (forwarder) name methodName. Then, when you call the forward method, those forwarder arguments get evaluated as a Tcl command call. So β if it's present β target is interpreted as a Tcl command (like a Tcl proc or an object, iykyk), and the rest of the forwarder arguments arg are the arguments passed into that command. The actual method arguments from the invocation of the forward method itself get appended to the list of forwarder arguments. β¨
And if target is omitted, the value of methodName is implicitly set and used as target. Which means: when you hand over a fully-qualified Tcl command name as methodName with no target, the unqualified methodName (the namespace tail) is used as the forwarder name, while the fully-qualified one serves as the target. Cute, right? π
Just like a regular object method, -returns lets you set a value checker on the values returned by the resulting Tcl command call. And when you pass an object to -frame, the resulting Tcl command is evaluated in the context of the object receiving the forward method call. That way, variable names used in the resulting execution of a command get resolved as object variables. Obsessed.
Wanna express that the forward method methodName is deprecated? Set the -deprecated flag. Deprecated methods stay usable from client code, but their usage will be signaled to the developer and/or can be tracked using ::nsf::deprecated. And to register methodName with the debugger, set the -debug flag. Entering and exiting a method that was flagged for debugging is recorded by calling the redefinable callback procs ::nsf::debug::call and ::nsf::debug::exit, respectively. By default those callbacks forward to ::nsf::log, which you can also customize at the script level. π
The list of forwarder arguments arg can contain, as its elements, a mix of literal values and placeholders. Placeholders are prefixed with a percent symbol (%) and get substituted for concrete values when you call the forward method. These placeholders are how you construct and manipulate the arguments being passed into the resulting command call on the fly. Main character energy:
%methodbecomes substituted for the name of the forward method, i.e.methodName.%selfbecomes substituted for the name of the object receiving the call of the forward method.%1becomes substituted for the first method argument passed to the call of the forward method. This requires, in turn, that at least one argument is passed along with the method call.
Alternatively, %1 accepts an optional argument defaults: {%1 defaults}. defaults must be a valid Tcl list of two elements. For the first element, %1 is substituted when there is no first method argument which can be consumed by %1. The second element is inserted upon availability of a first method argument, with the consumed argument being appended right after the second list element. This placeholder is typically used to define a pair of getter/setter methods.
{%@index value}becomes substituted for the specifiedvalueat positionindexin the forwarder-arguments list, withindexbeing either a positive integer, a negative integer, or the literal valueend(such as in Tcl'slindex). Positive integers specify a list position relative to the list head, negative integers give a position relative to the list tail. Indexes for positioning placeholders in the definition of a forward method are evaluated from left to right and should be used in ascending order.
Note that value can be a literal or any of the placeholders (e.g., %method, %self). Position prefixes are exempted, they are evaluated as %cmdName-placeholders in this context.
{%argclindex list}becomes substituted for the nth element of the provided list , with n corresponding to the number of method arguments passed to the forward method call.%%is substituted for a single, literal percent symbol (%).%cmdNameis substituted for the value returned from executing the Tcl commandcmdName. To pass arguments tocmdName, the placeholder should be wrapped into a Tcl list:{%cmdName ?arg ...?}.
Consider using fully-qualified Tcl command names for cmdName to avoid possible name conflicts with the predefined placeholders, e.g., %self vs. %::nx::self.
To disambiguate the names of subcommands or methods that potentially become called by a forward method, a prefix prefixName can be set using -prefix. This prefix is prepended automatically to the argument following target (i.e., a second argument), if present. If missing, -prefix has no effect on the forward method call. π
And to inspect and debug the conversions performed by all those placeholders above, setting the switch -verbose will have the command list to be executed (i.e., after substitution) printed using ::nsf::log (debugging level: notice) upon calling the forward method. Chef's kiss. π
πΈ info πΈ
obj info baseclass
Returns the base class of obj. The base class is the class from which all NX objects are instantiated directly or indirectly (typically nx::Object). π¦
obj info children ?-type className? ?pattern?
Retrieves the list of nested (or aggregated) objects of obj. The resulting list contains the fully qualified names of the nested objects. If -type is set, only nested objects which are direct or indirect instances of class className are returned. Using pattern, only nested objects whose names match pattern are returned. The pattern string can contain special matching characters (see string match). This method is how you introspect on contains. π₯
obj info class
Returns the fully qualified name of the current nx::Class of obj. In case of re-classification (see configure), the returned class will be different from the nx::Class from which obj was originally instantiated using create or new. Glow-up arc, honestly. β¨
obj info has ?mixin | namespace | type? ?arg ...?
obj info has mixin classNameβ Verifies whetherobjhas a givennx::ClassclassNameregistered as a mixin class (returns:true) or not (returns:false).obj info has namespaceβ Checks whether the object has a companion Tcl namespace (returns:true) or not (returns:false). The namespace could have been created using, for example,object require namespace.obj info has type classNameβ Tests whether thenx::ClassclassNameis a type of the object (returns:true) or not (returns:false). That is, the method checks whether the object is a direct instance ofclassNameor an indirect instance of one of the superclasses ofclassName.
obj info lookup submethod ?arg ...?
A collection of submethods to retrieve structural features (e.g. configuration options, slot objects) and behavioral features (e.g. methods, filters) available for obj from the perspective of a client to obj. Features provided by obj itself and by the classes in its current linearization list are considered. π
obj info lookup filter name
Returns the method handle for the filter method name, if currently registered. If there is no filter name registered, an empty string is returned. π
obj info lookup filters ?-guards? ?namePattern?
Okay so bestie, this one is giving you the method handles of all filters that are active on obj. π
By turning on the switch -guards, the corresponding guard expressions (if any) are also reported for each filter as a three-element list: methodHandle -guard guardExpr. The returned filters can be limited to those whose names match namePattern (see string match). β¨
obj info lookup method name
Returns the method handle for a method name if a so-named method can be invoked on obj. If there is no method name, an empty string is returned. π
obj info lookup methods ?namePattern?
Returns the names of all methods (including aliases and forwarders) which can be invoked on obj. The returned methods can be limited to those whose names match namePattern (see string match). π
obj info lookup mixins ?-guards? ?namePattern?
Returns the object names of all mixin classes which are currently active on obj. By turning on the switch -guards, the corresponding guard expressions (if any) are also reported as a three-element list for each mixin class: className -guard guardExpr. The returned mixin classes can be limited to those whose names match namePattern (see string match). π¦
obj info lookup parameters methodName ?namePattern?
Returns the parameter specification of the method methodName callable on obj as a list of parameter names and type specifications. The resulting parameter specification can be limited to those parameters whose names match namePattern (see string match). πΈ
obj info lookup slots ?-type className? ?-source all | application | system? ?namePattern?
Returns the command names of all slot objects responsible for managing properties, variables, and relations of obj. The returned slot objects can be limited according to any or a combination of the following criteria:
- First, slot objects can be filtered based on their command names matching
namePattern(see string match). - Second,
-typeallows one to select slot objects which are instantiated from a subclassclassNameofnx::Slot(default:nx::Slot). - Third,
-sourcerestricts slot objects returned according to their provenance in either the NX system classes or the application classes present in the linearization list ofobj(default:all).
To extract details of each slot object, use the info submethods available for each slot object. π
obj info lookup syntax methodName ?namePattern?
Returns the method parameters of the method methodName callable on obj as a concrete-syntax description to be used in human-understandable messages (e.g., errors or warnings, documentation strings). The result can be limited to those parameters matching the namePattern (see string match). π₯
obj info lookup variables
Returns the command names of all slot objects responsible for managing properties and variables of obj, if provided by obj or the classes in the linearization list of obj.
This is equivalent to calling: obj info lookup slots -type ::nx::VariableSlot -source all ?namePattern?.
To extract details of each slot object, use the info submethods available for each slot object. π
obj info name
Returns the unqualified name of an object, i.e., the object name without any namespace qualifiers. π
obj info info ?-asList?
Returns the available submethods of the info method ensemble for obj, either as a pretty-printed string or as a Tcl list (if the switch -asList is set) for further processing. π
obj info object filters ?-guards? ?pattern?
If pattern is omitted, returns all filter names which are defined by obj. By turning on the switch -guards, the corresponding guard expressions (if any) are also reported along with each filter as a three-element list: filterName -guard guardExpr. By specifying pattern, the returned filters can be limited to those whose names match patterns (see string match). β¨
obj info object method option methodName
This introspection submethod provides access to the details of methodName provided by obj. If methodName is not the name of an existing method, an empty string is returned. To disambiguate between a non-existing method and an empty string as valid return value (e.g., for info object method args|parameters|args|...), use info object method exists. π
Permitted values for option are:
argsβ returns a list containing the parameter names ofmethodName, in order of the method-parameter specification.bodyβ returns the body script ofmethodName.callprotectionβ returns the call-protection level set formethodName; possible values:public,protected,private.debugβ returns1ifmethodNameis in debug mode,0otherwise.definitionβ returns a canonical command list which allows for (re-)definemethodName.definitionhandleβ returns the method handle for a submethod in a method ensemble from the perspective ofobjas method provider.methodNamemust contain a complete method path.deprecatedβ returns1ifmethodNameis deprecated,0otherwise.existsβ returns1if there is amethodNameprovided byobj, returns0otherwise.handleβ returns the method handle formethodName.originβ returns the aliased command ifmethodNameis an alias method, or an empty string otherwise.parametersβ returns the parameter specification ofmethodNameas a list of parameter names and type specifications.registrationhandleβ returns the method handle for a submethod in a method ensemble from the perspective of the method caller.methodNamemust contain a complete method path.returnsβ gives the type specification defined for the return value ofmethodName.submethodsβ returns the names of all submethods ofmethodName, ifmethodNameis a method ensemble. Otherwise, an empty string is returned.syntaxβ returns the method parameters ofmethodNameas a concrete-syntax description to be used in human-understandable messages (e.g., errors or warnings, documentation strings).typeβ returns whethermethodNameis a scripted method, an alias method, a forwarder method, or a setter method.
obj info object methods ?-callprotection level? ?-type methodType? ?-path? ?namePattern?
Returns the names of all methods defined by obj. Methods covered include those defined using object alias and object forward. The returned methods can be limited to those whose names match namePattern (see string match). πΈ
By setting -callprotection, only methods of a certain call protection level (public, protected, or private) will be returned. Methods of a specific type can be requested using -type. The recognized values for methodType are:
scriptedβ denotes methods defined usingobject method;aliasβ denotes alias methods defined usingobject alias;forwarderβ denotes forwarder methods defined usingobject forward;setterβ denotes methods defined using::nsf::setter;allβ returns methods of any type, without restrictions (also the default value);
obj info object mixins ?-guards? ?pattern?
If pattern is omitted, returns the object names of the mixin classes which extend obj directly. By turning on the switch -guards, the corresponding guard expressions (if any) are also reported along with each mixin as a three-element list: className -guard guardExpr. The returned mixin classes can be limited to those whose names match patterns (see string match). π¦
πΈ obj info object slots
obj info object slots ?-type className? ?pattern?
Okay bestie, if you leave pattern out, this hands you back the object names of every single slot object defined by obj. But you can also narrow it down β and you can combine the criteria, it's giving choose your own adventure:
- First, slot objects get filtered by whether their command names match
pattern(peepstring matchfor the matching rules π¦). - Second,
-typelets you pick only the slot objects that are instantiated from a subclassclassNameofnx::Slot. And the default there?nx::Slot. Periodt.
π obj info object variables
obj info object variables ?pattern?
Skip the pattern and you get the object names of all the slot objects that obj provides β the ones doing the actual work of managing the properties and variables of obj. Give it a pattern and only the slots whose names match get returned. π
This is literally equivalent to calling:
obj info object slots -type ::nx::VariableSlot pattern
And if you wanna dig into the deets of any individual slot object, just use the info submethods that each slot object has. Iconic.
π¦ obj info parent
obj info parent
Returns the fully qualified name of obj's parent object, if it even has one. No parent? Then it reports the name of the Tcl namespace that contains obj β like "::". Sad little orphan moment but make it functional. π
β¨ obj info precedence
obj info precedence ?-intrinsic? ?pattern?
This one lists out all the classes obj inherits from β both the structural features (think properties) and the behavioral features (think methods) and methods β in the order that NX's linearization scheme puts them. π
Set the -intrinsic switch and you only get back the classes participating in superclass/subclass relationships (aka the intrinsic classes). Pass a pattern and only classes whose names match come through. And yes, that pattern string can contain the special matching characters (again, string match, you know the drill).
π obj info variable option handle
obj info variable option handle
Pulls selected details about a variable, identified by the given handle. Wanna know where a handle even comes from? Query obj with info object variables and info lookup variables. π
Valid values for option are:
nameβ returns the variable name.parameterβ returns a canonical parameter specification that's eligible to (re-)define the given variable (e.g. usingobject variable) in a new context.definitionβ returns a canonical representation of the definition command used to create the variable in its current configuration.
π obj info vars
obj info vars ?pattern?
Yields a list of Tcl variable names that were created and defined for the scope of obj β that's object variables, babe. You can limit the list to only the object variables whose names match pattern, and yes, pattern can contain those special matching characters (string match, always).
π₯ method
obj ?public | protected | private? object method ?-debug? ?-deprecated? name parameters ?-checkalways? ?-returns valueChecker? body
Defines a scripted method methodName for the scope of the object. Once you do this, the method becomes part of the object's signature interface. And besides a methodName, the method definition also specifies the method parameters and a method body. That's the whole trio. π«Ά
parameters accepts a Tcl list holding an arbitrary number of non-positional and positional parameter definitions. Each parameter definition is made of a parameter name, a parameter-specific value checker, and parameter options. So many layers, so much power.
The body is where the method implementation lives, as a script block. Inside that body script, the colon-prefix notation is available β that's how you denote an object variable and a self call. Plus, the context of the object receiving the method call (i.e. the message) can be accessed (e.g. using nx::self), and the call stack can be introspected (e.g. using nx::current). Main character introspection energy. π
Optionally, -returns lets you set a value checker on the values returned by the method implementation. And if you set the -checkalways switch, value checking on arguments and the return value is guaranteed to be performed, even if value checking is temporarily disabled; see nx::configure). No sneaky skips.
Wanna express that a method name is deprecated? Set the -deprecated flag. Deprecated methods stay usable from client code, but their usage gets signaled to the developer and/or can be tracked using ::nsf::deprecated. And to register name with the debugger, set the -debug flag. Entering and exiting a method that's been flagged for debugging gets recorded by calling the redefinable callback procs ::nsf::debug::call and ::nsf::debug::exit, respectively. By default those callbacks forward to ::nsf::log, which you can also customize at the script level. β¨
Now, a method closely resembles a Tcl proc, but bestie it differs in some important aspects:
- A method can define non-positional parameters and value checkers on arguments.
- The script implementing the method body can contain object-specific notation and commands (see above).
- Method calls cannot be intercepted using Tcl trace.
And a lil note for the record: an existing Tcl proc can be registered as an alias method with the object (see object alias). π¦
π move
obj move newObjectName
Effectively renames an object. Two-step process, no cap: first, the source object obj is cloned into a target object newObjectName using copy. Second, the source object obj is destroyed by invoking destroy. Gone, but reborn. πβ¨
Fun fact: move is also called internally when rename is performed for a Tcl command representing an object.
π mixins
obj object mixins submethod ?arg ...?
Accesses and modifies the list of mixin classes of obj using a specific setter or getter submethod. Let's break it down, one at a time. πΈ
obj object mixins add spec ?index?
Inserts a single mixin class into the current list of mixin classes of obj. Using index, you can set a position in the existing list of mixin classes where the new mixin class gets inserted. Omit it, and index defaults to the list head (0). Front of the line, always.
obj object mixins classes ?pattern?
Omit pattern and you get the object names of the mixin classes that extend obj directly. Specify pattern and the returned mixin classes get limited to the ones whose names match pattern (see string match).
obj object mixins clear
Removes all mixin classes from obj and returns the list of removed mixin classes. This clearing is equivalent to passing an empty list for mixinSpecList to object mixins set. Clean slate, fresh start. π§
obj object mixins delete ?-nocomplain? specPattern
Removes a mixin class from the current list of mixin classes of obj whose spec matches specPattern. That specPattern can contain special matching chars (see string match). Careful though: object mixins delete will throw an error if there's no matching mixin class β unless -nocomplain is set.
Lowkey a jump scare if you weren't expecting the error. Set -nocomplain if you'd rather it just vibe.
obj object mixins get
Returns the list of current mixin specifications. Simple, clean, serving.
obj object mixins guard className ?expr?
If expr is specified, a guard expression expr is registered with the mixin class className. This requires that the corresponding mixin class className has previously been set using object mixins set or added using object mixins add. And expr must be a valid Tcl expression (see expr). Passing an empty string for expr will clear the currently registered guard expression for the mixin class className.
If expr is not specified, it returns the active guard expression. And if none is available? You get an empty string back. π
obj object mixins set mixinSpecList
mixinSpecList represents a list of mixin class specs, and each spec is itself either a one-element or a three-element list: className ?-guard guardExpr?. If it has one element, that element gets considered the className of the mixin class. If it has three elements, that third element guardExpr gets stored as a guard expression of the mixin class. This guard expression will be evaluated using expr when obj receives a message, to determine whether the mixin is to be considered during method dispatch or not. Guard expressions let you realize context-dependent or conditional mixin composition. It's giving conditional main character energy. π
At the time of setting the mixin relation β that is, calling object mixins β every className that's part of a spec must be an existing instance of nx::Class. And to access and manipulate the list of mixin classes of obj, cget|configure -object-mixins can also be used. Two roads, same destination. π
β __object_configureparameter
obj __object_configureparameter
Computes and returns the configuration options available for obj, to be consumed as a method-parameter specification by configure. That's it, that's the whole job. π
πΈ property
obj object property ?-accessor public | protected | private? ?-class className? ?-configurable trueFalse? ?-incremental? ?-nocomplain? ?-trace set | get | default? spec ?initBlock?
okay so this one defines a property for the scope of the object π
The spec is where all the tea lives β it's a list holding at least one element, or maximum two: propertyName?:typeSpec? ?defaultValue?. That propertyName is also what gets used to form the names of the getter/setter methods, if you asked for those (see -accessor). It's optionally equipped with a typeSpec following a colon delimiter, and that specifies a value checker for the values that become assigned to the property. The second, optional element sets a defaultValue for this property. π
If -accessor is set, the property will provide for different getter and setter methods:
obj propertyName existsβ returns1if the value store ofpropertyName(e.g., an object variable) exists and has been given a value, and returns0otherwise.obj propertyName set valueβ sets the propertypropertyNameto value.obj propertyName getβ returns the current value of propertypropertyName.obj propertyName unsetβ removes the value store ofpropertyName(e.g., an object variable), if existing.
The option value you pass along -accessor sets the level of call protection for the generated getter and setter methods: public, protected, or private. By default? No getter and setter methods are created at all. π
Turning on the switch -incremental provides a refined setter interface to the value managed by the property. First, setting -incremental implies requesting -accessor (set to public by default, if not specified explicitly). Second, the managed value will be considered a valid Tcl list. A multiplicity of 1..* is set by default, if not specified explicitly as part of spec. Third, to manage this list value element-wise (incrementally), two additional setter methods become available:
obj propertyName add element ?index?β adding element to the managed list value, at the list position given by index (by default:0).obj propertyName delete ?-nocomplain? elementPatternβ removing the first occurrence of an element from the managed list value which matches elementPattern. elementPattern can contain matching characters (seestring match). An error will be thrown if there is no match, unless-nocomplainis set.
By setting -configurable to true (the default), the property can be accessed and modified through cget and configure, respectively. If false, no configuration option will become available via cget and configure.
If neither -accessor nor -configurable are requested, the value managed by the property will have to be accessed and modified directly. If the property manages an object variable, its value will be readable and writable using set and eval.
The -trace option causes certain slot methods to be executed whenever get, set, or default operations are invoked on the property:
- set:
slot value=set obj propertyName value - get:
slot value=get obj propertyName - default:
slot value=default obj propertyName
A property becomes implemented by a slot object under any of the following conditions:
-configurableequals true (by default).-accessoris one ofpublic,protected, orprivate.-incrementalis turned on.- initBlock is a non-empty string.
Assuming default settings, every property is realized by a slot object. no cap. π
Provided a slot object managing the property is to be created, a custom class className from which this slot object is to be instantiated can be set using -class. The default value is ::nx::VariableSlot.
The last argument initBlock accepts an optional Tcl script which is passed into the initialization procedure (see configure) of the property's slot object. See also initBlock for create and new.
By default, the property will ascertain that no (potentially) pre-existing and equally named object variable will be overwritten when defining the property. In case of a conflict, an error exception is thrown:
% Object create obj { set :x 1 }
::obj
% ::obj object property {x 2}
object ::obj has already an instance variable named 'x'
bestie that error is not a drill β the conflict check is real and it will absolutely catch you. π
If the switch -nocomplain is on, this check is omitted (continuing the above example):
% ::obj object property -nocomplain {x 2}
% ::obj eval {set :x}
2
require π«Ά
obj require namespace
Create a Tcl namespace named after the object obj. All object variables become available as namespace variables.
obj require ?public | protected | private? object method methodName
Attempts to register a method definition made available using ::nsf::method::provide under the name methodName with obj. The registered method is subjected to default call protection (protected), if not set explicitly.
unknown π
obj unknown unknownMethodName ?arg ...?
This method is called implicitly whenever an unknown method is invoked. unknownMethodName indicates the unresolvable method name, followed by the remainder of the original argument vector as a number of arg of the calling method invocation.
uplevel π
obj uplevel ?level? arg1 ?arg2 ...?
Evaluate a script or a command at a different stack-frame level. The command behaves in essence like Tcl's uplevel, but can be used to achieve identical results when filters or mixins are registered.
- If the level specifier is omitted,
uplevelwill skip any auxiliary frames added to the stack by active filters and mixins. The resulting stack-frame level corresponds to the callinglevel as indicated bynx::current. In this case methoduplevelcan be used to evaluate the command in the next enclosing procedure call, i.e., a frame corresponding to aproc, method, orapplycall, while skipping frames of filters and mixins. - If the level specifier is provided (relative, or absolute),
uplevelwill execute the command in the stack-frame level. In such cases, methoduplevelbehaves like Tcl'suplevelcommand.
% nx::Object create ::obj
::obj
% ::obj public object method foo {varName} {
:uplevel set $varName 1; return
}
::obj::foo
% namespace eval ::ns1 {
::obj foo BAR
}
% namespace eval ::ns1 {
info exists BAR
}
1
Note, in the example above, uplevel is guaranteed to resolve to the calling context of foo (ns1) despite mixins and filters being (potentially) registered on obj. iconic, honestly. β¨
upvar π¦
obj upvar ?level? otherVar1 localVar1 ?otherVar2 localVar2 ...?
Links one or more local variables to variables defined in other scopes (namespaces, objects, call frames). The command behaves in essence like Tcl's upvar, but can be used to achieve identical results when filters or mixins are registered.
- If the level specifier is omitted,
upvarwill skip any auxiliary frames added to the stack by active filters and mixins. The resulting stack-frame level corresponds to the callinglevel as indicated bynx::current. Therefore, methodupvargives access to the next enclosing procedure call, i.e., a frame corresponding to aproc, method, orapplycall, while skipping frames of filters and mixins. - If the level specifier is provided (relative, or absolute),
upvarwill link into the requested stack-frame level. In these cases, methodupvarbehaves like Tcl'supvarcommand.
% nx::Object create ::obj
::obj
% ::obj public object method foo {varName} {
:upvar $varName x; set x 1; return
}
::obj::foo
% namespace eval ::ns1 {
::obj foo BAR
}
% namespace eval ::ns1 {
info exists BAR
}
1
Note, in the example above, upvar is guaranteed to resolve to the calling context of foo (ns1) despite mixins and filters being (potentially) registered on obj. π«Ά
variable πΈ
obj object variable ?-accessor public | protected | private? ?-incremental? ?-class className? ?-configurable trueFalse? ?-initblock script? ?-trace set | get | default? ?-nocomplain? spec ?defaultValue?
okay so this one defines a variable for the scope of the object. The spec is where you hand over the variable specification, and it looks like variableName?:typeSpec?. The variableName is what names the underlying Tcl variable and the getter/setter methods, if you asked for those (peep -accessor). And spec can optionally come with a typeSpec after a colon delimiter β that's a value checker for the values this variable is allowed to hold. If you want, you can also define a defaultValue. β¨
Now, if -accessor is set explicitly, your variable will provide getter and setter methods:
obj variableName existsβ returns1if the value store ofvariableName(like, an object variable) exists and has been given a value; returns0otherwise. It's giving "do you even exist" energy. π«Άobj variableName set varValueβ setsvariableNametovarValue. Slay.obj variableName getβ returns the current value ofvariableName. Periodt.obj variableName unsetβ removesvariableName, if it exists, the one underlying the property.
The option value you pass along -accessor sets the level of call protection for the getter and setter methods: public, protected, or private. And by default? no getter and setter methods are created at all. None. Zero. Nada. π
Turning on -incremental gives you a refined setter interface to the value managed by the variable. Three things, bestie:
- Setting
-incrementalimplies requesting-accessor(publicby default, if you didn't specify it explicitly). - The managed value is considered a valid Tcl list. A multiplicity of
1..*is set by default, if you didn't specify it explicitly as part ofspec(see above). - To manage this list value element-wise (incrementally), two extra setter operations become available:
obj variableName add element ?index?β addselementto the managed list value, at the list position given byindex(by default:0).obj variableName delete ?-nocomplain? elementPatternβ removes the first occurrence of an element from the managed list value which matcheselementPattern.elementPatterncan contain matching characters (seestring match). An error will be thrown if there is no match β unless-nocomplainis set, in which case it's chill.
Set -configurable to true and the variable can be accessed and modified via cget and configure. If it's false (that's the default, btw), the interface based on cget and configure will not become available. In that case, and provided that -accessor is set, the variable can be accessed and modified via the getter/setter methods instead. Alternatively, the underlying Tcl variable β the one represented by the variable β can always be accessed and modified directly, e.g., using eval. By default, -configurable is false. No cap.
The -trace option causes certain slot methods to be executed whenever get, set, or default operations are invoked on the variable:
set:slot value=set obj variableName valueget:slot value=get obj variableNamedefault:slot value=default obj variableName
A variable becomes implemented by a slot object under any of these conditions:
-configurableequals true.-accessoris one ofpublic,protected, orprivate.-incrementalis turned on.-initblockis a non-empty string.
And assuming a slot object managing the variable is going to be created, you can set a custom class className for that slot object to be instantiated from, using -class. The default value is ::nx::VariableSlot. π
With -initblock you can define an optional Tcl script which gets passed into the initialization procedure (see configure) of the variable's slot object. See also initBlock for create and new.
By default, the variable will make sure that a pre-existing, equally named object variable does not get overwritten when you define the variable. And if there's a conflict, an error exception gets thrown, immediately:
% Object create obj { set :x 1 }
::obj
% ::obj object variable x 2
object ::obj has already an instance variable named 'x'
If the -nocomplain switch is on, though, this check is omitted (continuing the example above):
% ::obj object variable -nocomplain x 2
% ::obj eval {set :x}
2
π¦ OBJECT SELF-REFERENCE π¦
Objects are naturally recursive, bestie β methods of an object ::obj are frequently invoking other methods in that same object ::obj, and accessing ::obj's object variables. To represent these self-references effectively in method bodies, and depending on the usage scenario, NX offers two alternative notations for self-references: one based on a special-purpose syntax token (the "colon prefix"), and the other based on the command nx::current.
Heads up: both the colon-prefix notation andnx::currentmay be used only in method bodies and scripts passed toeval. If they appear anywhere else, an error will be reported. π¨
There are three main use cases for self-references:
- As a placeholder for the currently active object,
nx::currentcan be used to retrieve the object name. - Reading and writing object variables directly (i.e. without getter/setter methods in place) requires the use of variable names carrying the prefix
:("colon-prefix notation"). Internally, colon-prefixed variable names are processed using Tcl's variable resolvers. Alternatively, one can provide for getter/setter methods for object variables (seepropertyandvariable). - Self-referential method calls can be defined via prefixing (
:) the method names or, alternatively, vianx::current. Internally, colon-prefixed method names are processed using Tcl's command resolvers. The colon-prefix notation is recommended, also because it has a (slight) performance advantage overnx::current, which requires two rather than one command evaluation per method call. ποΈ
See the following listing for some examples corresponding to use cases 1--3:
Object create ::obj {
# 1) print name of currently active object ('::obj')
puts [current];
# 2) object variables
set :x 1; :object variable y 2;
:public object method print {} {
# 2.a) method-local variable
set z 3;
# 2.b) variable substitution using '$' and ':'
puts ${:x}-${:y}-$z;
# 2.c) reading variables using 'set'
puts [set :x]-[set :y]-[set z];
# 2.d) writing variables using 'set', 'incr', ...
set :x 1; incr :y;
}
:public object method show {} {
# 3.a) self-referential method call using ':'
:print;
# 3.b) self-referential method call using 'nx::current'
[current] print;
# 3.c) self-referential method call using 'nx::current object'
[current object] print;
}
:show
}
π COPYRIGHT π
Copyright (c) 2014-19 Stefan Sobernig <stefan.sobernig@wu.ac.at>, Gustaf Neumann <gustaf.neumann@wu.ac.at>; available under the Creative Commons Attribution 3.0 Austria license (CC BY 3.0 AT).
and that's a wrap, bestie!! πβ¨ this was nx::Object(3nx), version 2.4.0 β you made it to the very end of the page and I'm so proud of you fr fr π go forth and self-reference responsibly, mwah π¦π
π¦ where this page comes from
- debian trixie: nsf 2.4.0-1 Β· MIT
- ubuntu questing: nsf 2.4.0-1
- ubuntu resolute: nsf 2.4.0-1build1 Β· MIT
- ubuntu stonking: nsf 2.4.0-1build1 Β· MIT
- Copyright (c) 2014-19 Stefan Sobernig <stefan\&.sobernig@wu\&.ac\&.at>, Gustaf Neumann <gustaf\&.neumann@wu\&.ac\&.at>; available under the Creative Commons Attribution 3\&.0 Austria license (CC BY 3\&.0 AT)\&
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms