Mavnn's blog

Stuff from my brain

In Which Our Intrepid Author De-sugars a Monad by Hand

In my previous post ( http://blog.mavnn.co.uk/playing-with-error-handling-strategies ), Johann Deneux asked me whether my implementation of TryFinally actually worked.

TD;LR: It works. But thank you Johann for making me check!

Editted TD;LR: It’s broken, but not for the reason I thought. See the comments for details, and corrected code here http://blog.mavnn.co.uk/corrected-error-handling-computational-expres

This gave me a bit of a pause, as it’s actually an implementation I took from another source. One I trust, but it was still embarrassing to realise I couldn’t answer this question with full confidence as I didn’t quite understand the code myself.

In the end (especially given I’m planning to actually use this code in production at some point), I decided to go the whole hog and de-sugar a couple of simple versions of the computational expression.

So, first, the audit monad as used in my example code from the last post with just a simple let binding:

This raised my confidence slightly - the code I’d written did what I expected. But, what happened when I tried to bind a disposable resource using use! rather than let!? Time for the moment of truth:

So there you have it. The code does actually behave as expected, protecting the use of the disposable resource and ensuring it’s disposal. And I can breath easy that my code is doing what I thought it should be. Hopefully this will also serve as a useful example for anyone else starting out on the happy road of using computational expressions.

Comments