John Lam claims that C#'s "using" blocks aren't "extensible to let us do arbitrary things". He shows us an example of doing arbitrary things in statement blocks in Ruby. I'll agree that his example is slick, using a continuation to yield control to the block, but there's certainly nothing keeping you from getting same functionality in C#. The "using" block is simply a mechanism that utilizes the Dispose pattern to do things when you're done with them. I've done everything from the standard releasing of resources to implementing reader/writer locks with them.
To speak to John's example, there's nothing keeping you from creating a wrapper or subclass that calls SetInfo in the Dispose method to accomplish the same thing. Or, you could create a generic factory pattern that uses delegates to define the creation and disposing actions:
}
value = creator();
disposeAction(value);
This also incorporates thunk-like lazy creation. I'll admit to not having alot of Ruby experience, but it seems to me that "using" is every bit as "extensible" as far as executing code at the end of a block. Here's John's example using the above with some pseudo-code:
factory.Value.AccessRead =
factory.Path =
You could, of course, bake the delegates into a class to make it simpler. I suppose the most appropriate rebuttal to John's claims would be a wrapper class that takes care of it in the same way his example does, but I thought this example was a little more interesting.
John, I'd love to hear your feedback on this. I'm sure there's lots I could learn from you.
[Update] dasBlog swallowed my generics, so I had to fix them.
Remember Me
Page rendered at Sunday, October 12, 2008 12:46:49 AM (Pacific Standard Time, UTC-08:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.