How to get out of the closure mindset?

First, the obvious: In order to “get out of the closure mindset”, you should reduce your reliance upon closures.

Second: If you have no idea what state your program will be in when it gets somewhere, then you probably need to rethink your program.

Third: Enums and structs exist for a reason. Use them. A closure is a data structure with a function and the data needed to call it. The enum will mark what function you need - there are a finite number of them, and you know what they are when you create them. The structs (a different one for each function) can hold all the necessary data. Use a tagged union if you’re really feeling adventurous.

Remember, you knew what all the data was when you created the closure. This is the true message of Feoramund’s snippet. Don’t use function pointers when you can just select the proper one with a switch or series of if statements. Your code will be much more readable.

Fourth: Go read this thread: Closures are a poor man’s objects, and vice versa.
Now consider that Odin has neither closures nor objects. If your style of programming requires these, then Odin may simply not be a good fit for you at this time. But also consider that Odin doesn’t have these because they aren’t truly necessary. There are only data and functions that act on that data. Everything else is style and preference.

1 Like