Well, that’s exactly why I’m against using library functions for what should have been language constructs. It looks like a language construct, but behaves slightly different, which can be pretty confusing at times.
In this case, it obviously introduces an invisible this
into the scope, which can be annoying and confusing. It can be fixed by replacing this
with a thread-local variable, and it would also speed up things a bit because the resource collection can be reused. Not sure if that doesn’t bring any other confusion into the picture…
And this one has a very subtle bug: by using a list instead of a stack, it closes the resources in the same order they were opened. This is especially annoying because it doesn’t matter in most cases, and yet it is completely counter-intuitive. This could be easily fixed by using a stack, though.
Off topic: this forum needs hands-on help. I couldn’t figure out how to post code like that and how to edit the greyed-out parts of it (which would be needed if I wanted to demonstrate the this
problem).