Java’s `throws` keyword comes to mind. It forces you to handle exceptions or explicitly declare that you expect the exception to be handled by a method higher up the call stack.
Your strongly-typed suggestion seems better. But you’d have to somehow allow for cases where a function opens a file, does something with it, and also expects the file to be closed by a higher function. Perhaps something like:
PleaseDeferMe<File> BeginWrite() {
PleaseDeferMe<File> p = File.open(...);
// unsafe! Make sure to return p to the calling function so this gets called with “defer” at some point
File f = unwrapDefer p;
f.Write(some header)
return p
}
Your strongly-typed suggestion seems better. But you’d have to somehow allow for cases where a function opens a file, does something with it, and also expects the file to be closed by a higher function. Perhaps something like: