TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Rust RAII is better than the Haskell bracket pattern

238 pointsby setraover 6 years ago

9 comments

T-Rover 6 years ago
Something to keep in mind - linear types are on their way[1], with exactly this usecase in mind. Simon Peyton Jones gave an excellent presentation on the topic[2], briefly discussing exceptions, as well as giving a mention to ResourceT and the phantom type solution in the article (described as channel-passing).<p>[1] <a href="https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;1710.09756" rel="nofollow">https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;1710.09756</a> [2] <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=t0mhvd3-60Y" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=t0mhvd3-60Y</a>
评论 #18176843 未加载
评论 #18174854 未加载
kccqzyover 6 years ago
The thing is that, in Haskell, even when you attach a function to run during destruction, the runtime doesn&#x27;t guarantee that the function will be called promptly, or even at all. Rust drops (runs destructor and deallocates) values as soon as they go out of scope; C++ too. In Haskell you depend on the whims of the GC, which makes RAII unusable. (The Haskell approach of not guaranteeing destructors being called does have its merits; when many C++ and Rust programs are about to end, they spend the last few cycles uselessly deallocating memory that would&#x27;ve immediately been freed via _exit(2)).<p>Therefore the RAII style wouldn&#x27;t really work in Haskell. The current bracket approach is still better than RAII in Haskell.<p>That said, the ST-style trick of a phantom type variable is pretty well-known. Unfortunately not many people knew the same trick can be used for non-ST as well. I feel like as a community we should be encouraging this style more often.<p>UPDATE: I wrote the original comment with the incorrect assumption that drop functions will always be called in Rust. This is wrong. Please see child comments.
评论 #18174041 未加载
评论 #18175808 未加载
评论 #18174154 未加载
评论 #18175003 未加载
评论 #18174056 未加载
评论 #18177128 未加载
rbehrendsover 6 years ago
You can also have the exact opposite problem with RAII, where a resource survives the end of a transaction, because there is still a live reference to it hidden away somewhere (say, due to some debugging code holding on to it).<p>This is a classical liveness vs. safety dualism. &quot;Something good will eventually happen&quot; and &quot;nothing bad will ever happen&quot; are promises whose solutions are often in conflict with one another.<p>The general problem — to make transactional state changes and transactional control flow (i.e. expectations about these state changes) match up precisely — is not easy to solve in the general case, especially once you move on to things that are less trivial than simple resource acquisition&#x2F;release matching.
评论 #18175319 未加载
评论 #18175174 未加载
评论 #18175370 未加载
Animatsover 6 years ago
How do you handle errors at resource release? When you close a file, the final writes take place, and they can fail. What&#x27;s the idiom in Rust for getting them out?<p>Python&#x27;s &quot;with&quot; clause, and the way it interacts with exceptions, is the only system I&#x27;ve seen that gets this right for the nested case.
评论 #18174219 未加载
评论 #18174798 未加载
评论 #18174175 未加载
评论 #18174210 未加载
评论 #18175098 未加载
评论 #18174159 未加载
评论 #18175853 未加载
评论 #18174293 未加载
评论 #18174873 未加载
mark_l_watsonover 6 years ago
Interesting! Michael is one of the more prolific writings and practitioners in the Haskell space (I read just about everything he writes) so it is interesting to also read his take on Rust.
itover 6 years ago
Isn&#x27;t this just because withMyResource returns IO a rather than IO ()? It doesn&#x27;t seem reasonable for it to return the resource.
EugeneOZover 6 years ago
Would be interesting to know why memory leaks are possible in Rust, if RAII is so deeply integrated into language.
评论 #18174031 未加载
评论 #18174105 未加载
评论 #18174908 未加载
评论 #18174005 未加载
评论 #18174176 未加载
hardwaresoftonover 6 years ago
tl;dr - Try rust.<p>The mechanic point of this article is pretty clear:<p>- it&#x27;s possible to be unsafe in both Haskell and Rust when dealing with resource cleanup<p>- Rust does a bit of a better job in the general case though it has it&#x27;s own warts (see the other comments, it&#x27;s hard to deal with issues during `drop`-triggered cleanup)<p>I want to make a muddier meta point -- Rust is the best systems language to date (does anyone know a better one I can look at?).<p>- The person who wrote this article Michael Snoyman[0] is mainly a haskell developer, he&#x27;s the lead developer behind arguably the most popular web framework, yesod[1].<p>- Haskell developers generally have a higher standard for type systems, and spend a lot of time (whether they should or not) thinking about correctness due to the pro-activity of the compiler.<p>- These are the kind of people you <i>want</i> trying to use&#x2F;enjoy your language, if only because they will create&#x2F;disseminate patterns&#x2F;insight that make programming safer and easier for everyone down the line -- research languages (Haskell is actually probably tied for the least &quot;researchy&quot; these days in the ML camp) are the Mercedes Benz&#x27;s of the programming world -- the safety features trickle down from there.<p>- Rust is <i>not</i> a ML family language -- it&#x27;s a systems language<p>- People who write Haskell on a daily basis are finding their way to rust, because it has a pretty great type system<p>When was the last time you saw a systems language with a type system so good that people who are into type systems were working with it? When was the last time you saw a systems language that scaled comfortably and gracefully from embedded systems to web services? When have you last seen a systems language with such a helpful, vibrant, excited community (TBH I don&#x27;t think this <i>can</i> last), backed by an organization with values Mozilla&#x27;s?<p>You owe it to yourself to check it out. As far as I see it rust has two main problems:<p>- Learning curve for one of it&#x27;s main features (ownership&#x2F;borrowing)<p>- Readability&#x2F;Ergonomics (sigils, etc can make rust hard to read)<p>Admittedly, I never gave D[2] a proper shake, and I&#x27;ve heard it&#x27;s good, but the safety and the emphasis on zero-cost abstractions Rust offers me makes it a non-starter. Rust is smart so I can be dumb. C++ had it&#x27;s chance and it just has too much cruft for not enough upside -- there&#x27;s so much struggle required to modernize, to make decisions that rust has had from the beginning (because it&#x27;s so new). It might be the more stable choice for a x hundred people big corporate project today or next month, but I can&#x27;t imagine a future where Rust isn&#x27;t the premier backend&#x2F;systems language for performance critical (and even those that are not critical) programs in the next ~5 years.<p>I&#x27;ll go even one step further and say that I think that how much rust forces you to think about ownership&#x2F;borrowing and how memory is shared around your application is <i>important</i>. Just as Haskell might force you to think about types more closely&#x2F;methodically (and you&#x27;re often better for it), Rust&#x27;s brand of pain seems instructive.<p>[0]: <a href="https:&#x2F;&#x2F;www.snoyman.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.snoyman.com&#x2F;</a><p>[1]: <a href="https:&#x2F;&#x2F;www.yesodweb.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.yesodweb.com&#x2F;</a><p>[2]: <a href="https:&#x2F;&#x2F;dlang.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;dlang.org&#x2F;</a>
评论 #18175488 未加载
评论 #18175320 未加载
platzover 6 years ago
&quot;RAII&quot; is not &quot;Rust&quot;; &quot;the bracket pattern&quot; is not &quot;Haskell&quot;
评论 #18176310 未加载