Over years of game development, I’ve found a common pattern to follow is to hand a function a delegate that it must execute upon completion of whatever that function needs to do, like a callback function that aught to be invoked before a method exits.
I’ve also found that not calling a required callback can lead to bugs that are both difficult to recognize and identify.
After solving such a bug today, my colleague and I mused if there was a compiler-level way to enforce the invocation of a passed delegate. Similar to how out
parameters throw compiler errors if they are not set before the function terminates.
What I’m looking for is a compile-time way to guarantee that a delegate will be invoked before a function exits. I could not find one while searching documentation, but I might not know the right words to search for. I’d be fine with a solution that involved decorators/attributes, or even Roslyn compiler solutions. This just seems like the kind of thing that C# would handle natively.
Finally, if C# doesn’t have this, I would love examples from other languages which have keywords/patterns to guarantee method invocation.
Jake Muehle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.