3 ms·
It doesn't actually feel more readable to me. I find the idea that the lock declaration sits at the same level as the lock content confusing.
by yoavm 2y ago
It doesn't actually feel more readable to me. I find the idea that the lock declaration sits at the same level as the lock content confusing.
- zeroxfe 2y agoIt'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.
- nesarkvechnep 2y agoYes, it sits at the same level to signify the lifetime of the lock.