oo::util(3tcl)

πŸ“¦
version 2.0 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ NAME ✨

oo::util β€” the little utility command pack for TclOO, aka the glow-up your classes were waiting for πŸ’…

This is the <code>oo::util</code> manual page, section <code>3tcl</code>, bestie. Buckle up.


🌸 SYNOPSIS 🌸

package require Tcl 8.5 9

package require TclOO

package require oo::util ?1.2.3?

mymethod method ?arg...?

classmethod name arguments body

classvariable ?arg...?

link method...

link {alias method}...

ooutil::singleton ?arg...?

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so, this package hands you a convenience command so you can specify instance methods as callback commands, like, so easily. We're talking timers, file events, Tk bindings, etc. β€” all of it. ✨


πŸŽ€ COMMANDS πŸŽ€

πŸ’– mymethod method ?arg...?

This one lives inside instance methods. You give it a method name, and maybe some args for that method, and it hands you back a command prefix. When you run that prefix, it invokes the named method of the object you're in β€” with the args you gave it plus any others that show up at the actual moment of invocation. It's giving currying, no cap. πŸ“

Note: this command is equivalent to β€” and literally named after β€” the command that the OO package snit provides for the exact same purpose. iykyk.

🌈 classmethod name arguments body

This one lives inside class definitions. You hand it a method name, and possibly arguments for the method, and it creates a method on the class β€” available to a user of the class and of derived classes. Main character energy for your whole inheritance tree, fr fr. πŸ‘‘

Note: this is equivalent to the typemethod command that the OO package snit provides for the same purpose.

Example ⭐

oo::class create ActiveRecord {
    classmethod find args { puts "[self] called with arguments: $args" }
}
oo::class create Table {
    superclass ActiveRecord
}
puts [Table find foo bar]
# ======
# which will write
# ======
# ::Table called with arguments: foo bar

🧚 classvariable ?arg...?

This one lives inside instance methods. You give it a series of variable names and it makes them available in the method's scope. The originating scope for those variables is the class (instance) that the object instance belongs to. Translation: those referenced variables are shared between all instances of their class. Bestie, they're literally sharing. 🫢

Note: this is roughly equivalent to the typevariable command that the OO package snit provides for the same purpose. The difference? It cannot be used in the class definition itself. Important. Don't try it.

Example: 🌸

% oo::class create Foo {
    method bar {z} {
        classvariable x y
        return [incr x $z],[incr y]
    }
}
::Foo
% Foo create a
::a
% Foo create b
::b
% a bar 2
2,1
% a bar 3
5,2
% b bar 7
12,3
% b bar -1
11,4
% a bar 0
11,5

This one lives inside instance methods. You give it a list of method names and/or pairs of alias-name and method-name, and it makes the named methods available to all instance methods β€” no my command required. The audacity. Obsessed. πŸ”₯

The alias name under which the method becomes available defaults to the method name, except where you explicitly specify it through an alias/method pair.

Examples: ✨

    link foo
    # The method foo is now directly accessible as foo instead of my foo.

    link {bar foo}
    # The method foo is now directly accessible as bar.

    link a b c
    # The methods a, b, and c all become directly acessible under their
    # own names.

The main use of this command is expected to be in instance constructors, for convenience, or to set up some methods for use in a mini DSL. Slay. πŸ’œ

πŸ‘‘ ooutil::singleton ?arg...?

This command is a meta-class β€” that is, a variant of the builtin oo::class which ensures that it creates only a single instance of the classes defined with it. One. Uno. Periodt. πŸ’€

Syntax and results are like for oo::class.

Example: ⭐

% oo::class create example {
   self mixin singleton
   method foo {} {self}
}
::example
% [example new] foo
::oo::Obj22
% [example new] foo
::oo::Obj22

🫢 AUTHORS 🫢

Donal Fellows, Andreas Kupries


😭 BUGS, IDEAS, FEEDBACK 😭

This document, and the package it describes, will undoubtedly contain bugs and other problems. Not me being dramatic, it's just the truth of all software. Please report such in the category oo::util of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation. We want them. Give them to us. πŸ“

When proposing code changes, please provide unified diffs, i.e the output of diff -u.

Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. That's the whole trick, bestie. πŸ’…


🌈 SEE ALSO 🌈

snit(3tcl)


πŸ’– KEYWORDS πŸ’–

TclOO, callback, class methods, class variables, command prefix, currying, method reference, my method, singleton


⭐ CATEGORY ⭐

Utility


Copyright (c) 2011-2015 Andreas Kupries, BSD licensed


And that's the whole page, bestie β€” this was oo::util(3tcl) from <strong>tcllib</strong>, version <strong>1.2.3</strong>. Go forth and make some singletons. πŸ¦‹βœ¨

πŸ“¦ where this page comes from