3 ms·
It's readable if you're familiar with the RAII pattern which is used in languages like C++ and Go.
by zeroxfe 2y ago
It's readable if you're familiar with the RAII pattern which is used in languages like C++ and Go.
- saagarjha 2y agoI always felt that pattern was a bit too clever than it was a good design.
- karmarepellent 2y agoMakes me wonder what part of the pattern you think is "too clever"? I think it is fairly easy to reason about when the lock is restricted to the encompassing block and automatically dropped when you leave the block.
- saagarjha 2y agoIt’s kind of a weird design that some of your variables (which you can define anywhere in the scope FWIW) just randomly define a critical section. I strongly prefer languages that do a with lock { // do stuff } design. This could be C++ too to be honest because lambdas exist but RAII is just too common for people to design their locks like this.
- viraptor 2y agoIt's also similar to C#'s "using ...;" without a block. Syntax sugar there rather than RAII, but looks the same.
- zigzag312 2y agoC#'s using can be used without a block. It disposes the resource at the end of a current scope.
- wbl 2y agoIt's not RAII, but closer to dynamic-wind.
- Yoric 2y agoWell, Go doesn't quite support RAII. This syntax looks more like Python or Rust.