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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Crimes with Go Generics

172 点作者 psxuaw大约 3 年前

11 条评论

amscanne大约 3 年前
If the author is reading, I believe the problem noted at the end is actually that the code <i>isn’t</i> going through the thunk API and thus no memoization occurs. (Consider that there is no call to Set in the evaluation tree!)<p><pre><code> fib = func(n int) int { if cache[n].o.IsSome() { return *cache[n].o.val } return fib(n-1) + fib(n-2) } </code></pre> Should be:<p><pre><code> fib = func(n int) int { return cache[n-1].Force() + cache[n-2].Force() }</code></pre>
评论 #31152482 未加载
评论 #31151562 未加载
评论 #31150979 未加载
CraigJPerry大约 3 年前
Crimes with Go Generics really is the perfect title for this article.<p>I chuckled at the juxtaposition of the “expressivity and clarity” comment:<p>&gt;&gt; you can create composite types (types out of types). This lets you get a lot of expressivity and clarity about how you use Go<p>Followed by long winded code:<p>&gt;&gt; However, we can make this a lot more complicated with the power of the Thunk[T] type<p>And confusion:<p>&gt;&gt; Why is this so much slower?<p>This article touches on a lot of the frustration i see with abuse of static typing.<p>You’re going to write more code and take longer to write it and longer to change it (read: you’re going to cost me more money to build and maintain this code), for the unproven claim that you’ll write less bugs.<p>The author then goes on to write a logic bug in their memoization code.<p>But only after they’ve created a generic MPMC queue type by taking an already generic MPMC queue type (a go channel) then writing logic that breaks it (can’t handle an empty queue state) which they code their way out of by throwing more code i have to own.
评论 #31153784 未加载
评论 #31154467 未加载
reflexe大约 3 年前
Maybe I am missing something here but as someone who is not an expert in go generics, I don&#x27;t see any obvious &quot;crimes&quot; with the Option and Queue implementations.<p>I also could not find where the author explains their &quot;crimes&quot;.<p>What I am missing here?
评论 #31150976 未加载
评论 #31151530 未加载
svnpenn大约 3 年前
I think author has gotten out of hand with the avatars. They now have three talking to each other. One was fine, but at this point it&#x27;s just a distraction. Just write the article.
评论 #31157544 未加载
评论 #31157526 未加载
BreakfastB0b大约 3 年前
How long before someone figures out how to encode lightweight higher kinded types[1] in Golang. There shall be weeping and gnashing of teeth.<p>[1] <a href="https:&#x2F;&#x2F;ybogomolov.me&#x2F;01-higher-kinded-types&#x2F;" rel="nofollow">https:&#x2F;&#x2F;ybogomolov.me&#x2F;01-higher-kinded-types&#x2F;</a>
nirui大约 3 年前
&gt; Why is this so much slower?<p>Just a side note: Go test supports benchmarking (<a href="https:&#x2F;&#x2F;dave.cheney.net&#x2F;2013&#x2F;06&#x2F;30&#x2F;how-to-write-benchmarks-in-go" rel="nofollow">https:&#x2F;&#x2F;dave.cheney.net&#x2F;2013&#x2F;06&#x2F;30&#x2F;how-to-write-benchmarks-i...</a>) which gives you few useful tools such as `b.ReportAllocs()` and `go test -bench . -cpuprofile cpu.out -memprofile men.out`. These tools can give you far better information than just the execution time of a test case.
评论 #31155208 未加载
nx7487大约 3 年前
<p><pre><code> Go 1.18 added generics to the language. This allows you to have your types take types as parameters so that you can create composite types (types out of types). This lets you get a lot of expressivity and clarity about how you use Go. </code></pre> Aren&#x27;t structs already composite types?
评论 #31151323 未加载
评论 #31151919 未加载
评论 #31151168 未加载
mrlonglong大约 3 年前
Iain M. Banks once wrote this: &quot;It&#x27;s brains, not gonads that matters&quot;.
Koiwai大约 3 年前
The fibonacci should use an array&#x2F;slice for storage instead of a map(hashmap).
Kostarrr大约 3 年前
I don’t think the Option presented here is sufficient (it is indeed a “crime”) as you cannot express Some(nil).<p>But you could probably achieve that using go&#x27;s not-well-known sum types, e.g. like this<p><a href="https:&#x2F;&#x2F;github.com&#x2F;FSMaxB&#x2F;type-safe-builder-experiment&#x2F;blob&#x2F;master&#x2F;go&#x2F;builder&#x2F;staticOptional.go" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;FSMaxB&#x2F;type-safe-builder-experiment&#x2F;blob&#x2F;...</a>
评论 #31151449 未加载
评论 #31151338 未加载
评论 #31151468 未加载
评论 #31155015 未加载
RamblingCTO大约 3 年前
The design isn&#x27;t very reader-friendly to be honest. I really don&#x27;t like the trend of terminal-inspired designs. Yeah, looks nice and nerdy, but isn&#x27;t really useful for reading. Monospaced fonts aren&#x27;t very good for running text as well.