defer(n)

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

✨ 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 to defer::cancel later 🫢
  • ::defer::with variableList script β€” defers execution of a script, but first copies the current value of some variables (the ones named in variableList) into the script. the script acts like a lambda but executes at the same level as the defer::with call. return value is the same as ::defer::defer ✨
  • ::defer::autowith script β€” literally the same as ::defer::with except 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 (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