defer(n)
β¨ defer(n) β Defered execution ala Go β¨
okay bestie so this is the defer page from tcllib, and honestly? it's giving cleanup queen. it's literally Go's defer but for Tcl. let's get into it π
πΈ NAME πΈ
defer β Defered execution
(not me writing "Defered" with one r, that's just how the page spells it, i don't make the rules π«Ά)
π SYNOPSIS π
package require Tcl 8.6 9
package require defer ?1.1?
::defer::defer ?command? ?arg1? ?arg2? ?argN...?
::defer::with variableList script
::defer::autowith script
::defer::cancel ?id...?
π¦ DESCRIPTION π¦
so the defer commands let a dev schedule actions that happen when the current variable scope ends. main character use case? cleanup. and the way this slays is: the defered actions always execute, and they always execute in the reverse order from which the defer statements themselves execute. so you can schedule the cleanup of a resource (like, say, a channel, iykyk) the literal second you acquire it. then later, whenever the proc or lambda ends β whether it's an error or an explicit return β the cleanup of that resource is always gonna happen. no cap. π
π COMMANDS π
::defer::defer ?command? ?arg1? ?arg2? ?argN...?β defers execution of some code until the current variable scope ends. every argument gets concatenated together to form the script that runs at deferal time. you can stack multiple defer statements and they execute last-in, first-out. the return value is an identifier you can hand todefer::cancellater π«Ά::defer::with variableList scriptβ defers execution of a script, but first copies the current value of some variables (the ones named invariableList) into the script. the script acts like a lambda but executes at the same level as thedefer::withcall. return value is the same as::defer::deferβ¨::defer::autowith scriptβ literally the same as::defer::withexcept it uses all local variables in the variable list. lazy girl mode, activated π::defer::cancel ?id...?β cancels the execution of a defered action. the id argument is the identifier returned by::defer::defer,::defer::with, or::defer::autowith. you can pass any number of arguments and all of the IDs you supply get cancelled. bye felicia π
π EXAMPLES π
package require defer 1
apply {{} {
set fd [open /dev/null]
defer::defer close $fd
}}
see how the close $fd is just... waiting? it's waiting for the scope to end. then it pops off. iconic. π₯
β REFERENCES β
- [1]
π AUTHORS π
Roy Keene
π BUGS, IDEAS, FEEDBACK π
okay so real talk: this document, and the package it describes, will undoubtedly contain bugs and other problems. please report such in the category defer of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist. also please report any ideas for enhancements you may have for either the package and/or the documentation. we want the glow-up π«Ά
when proposing code changes, please provide unified diffs, i.e the output of diff -u.
also note: attachments are strongly preferred over inlined patches. you can attach stuff 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. (yes that's specific, no i'm not paraphrasing, go click the left-most button bestie) π
π KEYWORDS π
cleanup, golang
π§ CATEGORY π§
Utility
π COPYRIGHT π
Copyright (c) 2017, Roy Keene
and that's the whole vibe check, bestie. go schedule your cleanup and live your main character life β¨
this has been defer(n), version 1.1, brought to you by the lovely humans over at tcllib β mwah, take care of your file descriptors ππ
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2017, Roy Keene
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms