3 ms·
There is one downside, though it's not inherent to garbage collection, it just happens to correlate in current languages. Gc'd languages don't have raii, and fo
by im3w1l 2y ago
There is one downside, though it's not inherent to garbage collection, it just happens to correlate in current languages. Gc'd languages don't have raii, and for that reason they actually make you do more work when managing non-memory resources. The have been some attempts to work around this with e.g. pythons with, but In my opinion it's less ergonomic due to forcing indentation.
- vbezhenar 2y agoGo's defer works like RAII and does not introduce indentations. f := createFile("/tmp/defer.txt") defer closeFile(f) writeFile(f)
- rcxdude 2y agoDefer and context managers are a pale approximation of RAII. They are constrained to lexical scope, as opposed to RAII which works with object lifetimes. And it's the latter case which is much more error prone than 'oops, I forgot to close a file I opened this function'.
- seabrookmx 2y agoEven when the syntax does require indentation ("with" in python, "using" in C#) it's still pretty clean IMO.
- david_allison 2y agoAdditionally, `using` no longer requires indentation in C# https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/using https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
- seabrookmx 2y agoTIL! I just recently updated a few small services to C# 12 and there's a bunch of little niceties like this I'm finding (spread operator for instance).
- pjmlp 2y agoUsing also no longer requires inheritance from IDispose, it suffices that the type does support Dispose pattern, which is great when coupled with extension methods.
- HideousKojima 2y agoUsing no longer requires a new scope block in more recent versions of C#
- valicord 2y agoThis misses the main benefit of RAII which is that you can't forget to close the file.
- pjmlp 2y agoWhile not perfect, static analysis takes care of that, in similar veins C and C++ devs reach out to them for the language flaws.
- neonsunset 2y agoDo they? // Disposed when goes out of scope using var http = new HttpClient(); var text = await http.GetStringAsync("https://example.org/");
- pjmlp 2y agoThey surely do, D for example. Swift as another one, RC is a GC algorithm, before the usual clueless reply from people not educated in CS. Followed by FP languages now adopting effects and linear type systems, alongside their automatic memory management approaches.