If you want it to run on exiting the function in reverse order, something like Python's context managers would probably be necessary.
I've dabbled in that too, though I don't actually use it for anything, currently: https://github.com/Mcsavvy/contextlib/pull/1