TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Higher RAII and the seven arcane uses of linear types

99 点作者 agluszak大约 1 年前

7 条评论

verdagon大约 1 年前
Cool to see this on HN!<p>This is my favorite part of language design: you get to play with weird ideas and see how the implications cascade through the rest of the program. It kind of blew my mind when a <i>linear type</i> solved a <i>caching</i> problem.<p>Now it&#x27;s kind of a curse, because any time I code in a normal language (even the modern ones like Scala and Rust) I see places (concurrency, coroutines, etc) where we could have statically prevented these bugs.
评论 #40362769 未加载
评论 #40361226 未加载
评论 #40361971 未加载
评论 #40362318 未加载
two_handfuls大约 1 年前
One thing I’m missing: how does the compiler check that the “destruct” function is called? Can’t the program just accidentally stash the linear type in some list and forget about it?
评论 #40363332 未加载
评论 #40367749 未加载
评论 #40363467 未加载
kccqzy大约 1 年前
In the transaction example, if this were C++, we could probably make the actual destructor private, and then have commit() and rollback() functions take the transaction object by rvalue reference (needs a layer of unique_ptr here). What am I missing? Is this a problem because it forces unique_ptr?
评论 #40362338 未加载
miki123211大约 1 年前
You can implement a weaker version of this in any language that has either destructors or finalizers. Have every linear struct contain a private `was_consumed` field, false by default. Every method that consumes such a struct should set that field to true. If your language doesn&#x27;t have single-owner references, such methods should also panic if the field is already true.<p>Depending on how closely you subscribe to the &quot;let it crash&quot; philosophy, your destructor should do anything from writing a log to outright panicking and crashing your program when encountering a false value. You can even put all of this under an #ifdef so that it only runs in debug.<p>This isn&#x27;t as good as actual linear types, as errors will only appear at runtime and only for the code paths you actually hit in development, but it&#x27;s better than nothing.
gpderetta大约 1 年前
Great article, but I thought that linear types already implied &quot;named destructors&quot;, hence the &quot;Higher RAII&quot; moniker is redundant. If not, how do languages with linear types handle a linearly typed object lifetime?
NeutralForest大约 1 年前
Is there a reason we&#x27;re not seeing linear types in more languages? I&#x27;m not familiar with how difficult it is to implement or if there are usage issues that end up showing only after a while?
评论 #40361183 未加载
评论 #40361782 未加载
评论 #40361487 未加载
IcyWindows大约 1 年前
Don&#x27;t private destructors in c++ enable this?
评论 #40362323 未加载
评论 #40362064 未加载