3 ms·
Personally I would call this “execute within context”. The context could be a database transaction, or saving/restoring graphics state, or swapping out some glo
by pavlov 3y ago
Personally I would call this “execute within context”. The context could be a database transaction, or saving/restoring graphics state, or swapping out some global state to control instrumentation, or whatever.
The destructor approach has one advantage for semantic clarity: it’s guaranteed that your execute-within-context block really executes immediately and just once. Passing a lambda to a function means the function could call the lambda now, or later, or ten times, or maybe never. Still, I think it’s a more readable approach for contexts that are not holding resources.
- marcosdumay 3y ago> Personally I would call this “execute within context”. Oh, a monad. Yeah, a monad solves it. But it's quite overkill.
- mrkeen 3y agoIt would only be overkill if you had to invent it yourself. But since there's an existing solution, use that. The article went from destructors to the with-pattern. Once you start thinking about exceptions, you might reach for try-with-resources. With Async exceptions you might look at bracket. If you want composability, go for ResourceT.
- marcosdumay 3y agoBasically everything on the article is overkill. You just need to compose the XML contents declaratively... what is the most natural way to compose data. The original API goes out of its way to be dangerous and hard to use.