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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Phantom Types in Rust

75 点作者 0x1997将近 12 年前

5 条评论

tel将近 12 年前
Phantom types are used frequently in Haskell to really great effect. Perhaps the most well-known example is the ST monad which allows you to do mutable, destructive updates in a &quot;pure&quot; fashion by containing them all within a narrow region. From outside of that region, the effects are invisible and referentially transparent. This is great for embedding your favorite iterative&#x2F;mutable algorithm in a pure program.<p>Phantom types along with quantified type erasure allow the compiler to force these impure regions to be contained. They disallow sharing of variables or results between ST monad invocations statically.<p>To do so, most operations are polymorphic in the phantom variable. So something like `get (put x box)` is of type `ST s Int` for an `x :: Int`. The `s` corresponds to whichever ST region this action gets run within.<p>Running ST looks like `runST :: (forall s. ST s a) -&gt; a` which means that the ST action you give to ST must be completely naive in its choice of region. Another way of thinking about it is that the `s` parameter is chosen adversarially by the compiler so that only ST actions which are able to be run in any region are allowed to be run at all.
yebyen将近 12 年前
I loved this blog. BluishCoder was my goto for Factor articles when I was learning about Factor. At least I thought it was. Browsing the titles under the Factor tag, I don&#x27;t find one article I recognize, and all of the links I&#x27;ve tried clicking through wind up at nginx 404.<p>Sadly! Books and their covers... I&#x27;m sure they were good.<p>I did find the article I was thinking of, but today keyword search has failed me. I was looking for &quot;monotonically increasing timers&quot; and the correct search was &quot;fast now&quot;<p><a href="http://re-factor.blogspot.com/2011/03/fast-now.html" rel="nofollow">http:&#x2F;&#x2F;re-factor.blogspot.com&#x2F;2011&#x2F;03&#x2F;fast-now.html</a><p>Chris D, are you here reading comments? Can you bring back your Factor articles?
评论 #6217501 未加载
valtron将近 12 年前
(I don&#x27;t know Rust)<p>So I assume the compiler infers the type of `TI(1)` as `T&lt;int&gt;` and `TS(~&quot;Hello&quot;)` as `T&lt;~string&gt;` because ...?<p>What if you defined:<p><pre><code> enum T&lt;A&gt; { TX(int, ~string), TY(~string, int) } </code></pre> What&#x27;s the inferred type of `TX(1, ~&quot;a&quot;)`?
评论 #6219283 未加载
评论 #6221255 未加载
Anderkent将近 12 年前
How does the compiler know that TI(1) is of type T&lt;int&gt;, and TS(&quot;foobar&quot;) is T&lt;~string&gt;? Does it infer the type of the enum constructors from their arguments? Does that mean the arities must be the same?<p>Say something like:<p>enum Node&lt;A&gt; { StringNode(Node, Node, ~string) IntNode(Node, Node, int) VoidNode(Node, Node) }<p>Is there any way of declaring that StringNode gives you Node&lt;~string&gt;, and IntNode gives Node&lt;int&gt;?
评论 #6219025 未加载
评论 #6219939 未加载
评论 #6219176 未加载
Symmetry将近 12 年前
Does anyone here know why they call those structures Enums rather than Unions?
评论 #6217765 未加载
评论 #6220132 未加载
评论 #6218503 未加载
评论 #6217635 未加载