CheeseLord / plaid

PLAtformer In D
MIT License
0 stars 0 forks source link

Ensure that if there's a failure during setup, we clean up everything that was setup and nothing more #30

Open kronmillerg opened 8 years ago

kronmillerg commented 8 years ago

Proposed way of organizing these things: each module that needs initialization should have setupX and cleanupX functions. Each setupX should guarantee that if it fails, it will return false and clean up everything that it partially set up (possibly using some clever scope(failure) clauses). The master setup function should call the setup functions in order, and if one of them returns false it should not call any later setup functions, instead call all of the previous cleanup functions in reverse order, and return false.

kronmillerg commented 8 years ago

Or we could just say that it's safe to call a module's cleanup function even if its setup function was never called, but that seems like it'd be annoying.

kronmillerg commented 8 years ago

It also occurs to me that setupProgress (from setup_cleanup.d) doesn't need to be a global. We could instead make it local to setup(), and in the case where setup fails, setup() could call a function cleanupHelper() which is passed the setupProgress, and cleanup() could just call cleanupHelper(EVERYTHING_INITIALIZED).