oo::util(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– oo::util β€” Utility commands for TclOO πŸ’–

okay bestie, so you know how TclOO is already iconic but sometimes you just want it to do a little bit more? yeah. this is that. oo::util is the utility pack, it ships with tcllib (this page is version 1.2.2, and the manual itself is oo::util(n) if you're keeping track), and it hands you a bunch of lil convenience commands so you can vibe with callbacks, class methods, class variables and singletons without suffering. no cap.


✨ NAME ✨

oo::util β€” Utility commands for TclOO. that's the whole name. short, sweet, slays.


πŸŽ€ SYNOPSIS πŸŽ€

package require Tcl 8.5 9

package require TclOO

package require oo::util ?1.2.2?

mymethod method ?arg...?

classmethod name arguments body

classvariable ?arg...?

link method...

link {alias method}...

ooutil::singleton ?arg...?

πŸ¦‹ DESCRIPTION πŸ¦‹

so like, this package gives you a convenience command for the easy spec of instance methods as callback commands. you know the vibe β€” timers, file events, Tk bindings, etc. the whole "pass me a command prefix and I'll call it later" situation. it's giving ✨ergonomic✨. obsessed.


πŸ’… COMMANDS πŸ’…

mymethod method ?arg...?

bestie listen. this one lives inside instance methods. you give it a method name and, possibly, some arguments for that method, and it hands you back a command prefix. when that prefix actually gets executed, it will invoke the named method of the object you're currently in, using the arguments you provided plus any extra ones tacked on at the time of actual invocation. currying but make it cute. πŸ“

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

classmethod name arguments body

this one is available within class definitions. you give it a method name, plus arguments and a body, and it creates a method on the class. that method is then available to a user of the class and of any derived classes. class-level rizz, honestly. πŸ‘‘

Note: it's equivalent to the command typemethod provided by the OO package snit for the same purpose. same energy, different name.

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

not me getting emotional over an inherited class method. πŸ₯Ή

classvariable ?arg...?

available within instance methods, bestie. you pass 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 (the instance) that the object instance belongs to. translated: the referenced variables are shared between all instances of their class. every single one. it's a group chat, not a DM. πŸ’¬

Note: roughly equivalent to the command typevariable provided by the OO package snit. the difference is that it cannot be used in the class definition itself. so don't try it there, it won't work, and you will cry.

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

see how a and b are sharing? that's the whole point. they're both clocking into the same variable. πŸ’–

available within instance methods. you hand it a list of method names and/or pairs of alias-name and method-name, and it makes those named methods available to all instance methods β€” without you having to type the my command every time. life-changing. literally. πŸ™ˆ

the alias name under which the method becomes available defaults to the method name, except where you explicitly specify one 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. tiny language, huge slay. 🌈

ooutil::singleton ?arg...?

okay so this one is a meta-class β€” that means it's a variant of the builtin oo::class which makes sure it creates only a single instance of the classes defined with it. one (1) instance. no more. no duplicates. the main character energy of classes. πŸ‘‘

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

look at that. same object both times. ::oo::Obj22 twice. iconic behaviour. ✨


🌸 AUTHORS 🌸

Donal Fellows, Andreas Kupries. legends, both of them. 🫢


😭 BUGS, IDEAS, FEEDBACK 😭

ngl, this document β€” and the package it describes β€” will undoubtedly contain bugs and other problems. it's software, bestie. so please report such in the category oo::util of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist. please also report any ideas for enhancements you may have for either the package and/or the documentation. we want the ideas. give us the ideas. πŸ’Œ

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. don't inline your patch, it's giving chaos. 🫠


πŸ‘€ SEE ALSO πŸ‘€

  • snit(n) β€” the OO package that inspired half of this, go read it too

⭐ 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


okay that's the whole page!! πŸ’– this was oo::util(n), version 1.2.2, straight outta tcllib. go forth and curry your callbacks, bestie. periodt. πŸŒˆβœ¨πŸ¦‹

πŸ“¦ where this page comes from