3 ms·
Why would you treat the two differently, though? What benefit does it bring? (defer is such an ugly, manual solution in general; it becomes very cumbersome once
by Sesse__ 1y ago
Why would you treat the two differently, though? What benefit does it bring? (defer is such an ugly, manual solution in general; it becomes very cumbersome once you may want to give control of the resource to anyone else.)
- sirwhinesalot 1y agoWell, the answer is obvious in garbage collected languages: because a GC excels at managing memory but sucks at managing other resources. Here, the answer is that ownership semantics are disliked by the language designer (doesn't fit the design goals), so they're not in the language.
- pjmlp 1y agoNot really obvious, given that there are garbage collected languages with RAII, like D, to quote one example. And even stuff like try/using/with can be made RAII alike, via static analysis, which defer like approaches usually can't, because it can be any expression type, unlike those other approaches that rely on specific interfaces/magic methods being present, thus can be tracked via the type system. So it can be turned into a compiler error if such tagged type doesn't escape lexical scope without calling the respective try/using/with on the variable declaration.
- sirwhinesalot 1y agoNot disagreeing, just pointing out the reasoning.
- pjmlp 1y agoFair enough, also wanted to make a point of the possibilities, given that too many people place all GC languages on the same basket.