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.

Calculating Go type sets is harder than you think

79 pointsby Meroviusabout 3 years ago

4 comments

TheDongabout 3 years ago
&gt; Go has the goal of having a fast compiler. And importantly, one which is guaranteed to be fast for any program.<p>&gt; ...<p>&gt; This is especially important for environments like the Go playground, which regularly compiles untrusted code.<p>To be clear here, the tradeoff being made is that it is more important that the compiler is fast than that it is able to tell programmers about clear bugs in their code.<p>I see the justification of the playground as unconvincing too. You can run &#x27;for {}&#x27; in the playground, and you&#x27;ll get &quot;timeout running program&quot;. They already have solved the problem for the playground because obviously at runtime a go program can take infinite time, and it doesn&#x27;t seem like it&#x27;s any harder to also constrain compilation in the same way.<p>The actual cost that the go compiler being fast has is that other parts of go are slow. In other languages, a macro will execute as part of compilation, and incremental compilation will work with it. In go, if you want a macro, you write a separate go program that generates code, write a &quot;&#x2F;&#x2F;go:generate&quot; comment, and then have no way of knowing when to run it, no way of having incremental compilation with it, and it invariably ends up being slower than the compiler expanding it itself would have been.<p>Now, if you want to know if you have an impossible type set, you&#x27;ll run &quot;go vet&quot; (which likely doesn&#x27;t do this validation, I haven&#x27;t checked) or one of the various linting tools, it&#x27;ll have to re-parse and re-compile all the code, it won&#x27;t cache, and it&#x27;ll ultimately give you the same answer the compiler could have given you.<p>I suppose this is superior in one way: when performing such generation and validation, you can do it against only your own code, rather than against third party code you import, while if it were part of the compiler directly it would typically end up running against all compilation units, even third party ones....<p>In practice though, even for &quot;slow to compile&quot; languages, I find that I spend more time finding and fixing bugs than I do waiting for compilation, especially with incremental compilation. On the other hand, with go, I spend less time total compiling code, more time total fixing bugs, and more time total running golangci-lint and various other bits of code-generation.<p>I spend exactly 0 time worrying that some adversarial third-party dependency I import will make my compile-times exponential, both in go where it can&#x27;t happen by design, but also in other languages where it could happen, but you know, doesn&#x27;t.
评论 #31419390 未加载
评论 #31420620 未加载
评论 #31420371 未加载
评论 #31421146 未加载
评论 #31431633 未加载
评论 #31432329 未加载
评论 #31419513 未加载
jrockwayabout 3 years ago
&gt; The compiler accepts it just fine, though.<p>I&#x27;ve never seen a concrete type in an interface before, so I decided to try it:<p><pre><code> package main type A interface { int M() } func f(x A) { &#x2F;&#x2F; line 8 x.M() } </code></pre> It is indeed accepted if it&#x27;s there by itself, but it is rejected if you try to use the interface, which is what function f does:<p><pre><code> .&#x2F;main.go:8:10: interface contains type constraints </code></pre> I think this ends up being a very boring reason for rejection and is somewhat irrelevant to the post that you can&#x27;t actually put concrete types in interface{} definitions. It looked weird to me so I had to try it.<p>Since this is in the context of generics, I think the author might have been thinking about embedding types like comparable, which are interfaces (comparable is interestingly defined as `type comparable interface { comparable }`). You can, of course, put interfaces in interfaces in general (see io.ReadCloser and friends).<p>For the comparable case:<p><pre><code> type A interface { comparable M() } func p(x A) { x.M() } </code></pre> This gets its own error:<p><pre><code> .&#x2F;main.go:8:10: interface is (or embeds) comparable </code></pre> Very clever, because yeah, there is nothing stopping you from writing `func f(x comparable) {}`. Except this explicit check.<p>I don&#x27;t know what the point of this comment is, since 99% of the article is not about this code block, but I guess I found it interesting.
评论 #31419493 未加载
评论 #31432383 未加载
评论 #31421917 未加载
choegerabout 3 years ago
It&#x27;s astonishing how hard &quot;modern&quot; PL find it to implement parametric polymorphism. Java got it wrong and C# did at least have a hard time. I don&#x27;t want to talk about what C++ did. Go needed years for that feature and now has problems with type constraints (I am not a go expert, though. Maybe these constraints aren&#x27;t related to their generics?). So far, Rust seems to have gotten it right by essentially copying Haskell.<p>Is it pride that let&#x27;s &quot;industry&quot; languages ignore decades of PL theory and research? The &quot;design by committee&quot; problem? Or something else entirely?
评论 #31419825 未加载
评论 #31420423 未加载
评论 #31420230 未加载
评论 #31419674 未加载
评论 #31419638 未加载
评论 #31424313 未加载
评论 #31432406 未加载
评论 #31419793 未加载
评论 #31420120 未加载
评论 #31419645 未加载
dolmenabout 3 years ago
I don&#x27;t get why the compiler should have to check if the type set is non-empty. The compiler only has to check when the generic type&#x2F;func is used if the concrete type matches the type set. At worse the developer has written generic code that nobody can use.<p>Checking type sets is a work for a linter. But the developer should have caught the problem with a simple unit test.<p>I&#x27;m glad that the specification relieves the compiler writers from that the non-empty check task.
评论 #31432317 未加载
评论 #31431653 未加载