LWN: Comments on "Generics for Go" https://lwn.net/Articles/824716/ This is a special feed containing comments posted to the individual LWN article titled "Generics for Go". en-us Tue, 30 Sep 2025 23:24:05 +0000 Tue, 30 Sep 2025 23:24:05 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Generics for Go https://lwn.net/Articles/828489/ https://lwn.net/Articles/828489/ mcortese I'll tangentially note that a problem with 3 horns is not a "dilemma". Mon, 10 Aug 2020 23:14:21 +0000 Generics parameter syntax https://lwn.net/Articles/826392/ https://lwn.net/Articles/826392/ anton I expect that symbol table lookup in the parser is not a performance problem. It is a conceptual problem because you need to know the parse tree to know the scope of an identifier; of course, for some kinds of languages the problem is solvable (LL(1) languages with definitions before use of symbols should be fine), but it means that the language requires an integration of parsing and "static semantics". And possibly scanning, if, e.g., the syntax requires a token for a type that's separate from an identifier token. Fri, 17 Jul 2020 17:08:32 +0000 Generics for Go https://lwn.net/Articles/826240/ https://lwn.net/Articles/826240/ jch <div class="FormattedComment"> <font class="QuotedText">&gt; there was a language ML</font><br> <p> Why in the past tense? ML is doing fine, the dialect with the most active community would appear to be OCaml, <a rel="nofollow" href="https://ocaml.org/">https://ocaml.org/</a> . (It looks to me like ML was an important inspiration for Rust.)<br> </div> Thu, 16 Jul 2020 10:17:49 +0000 Generics for Go https://lwn.net/Articles/825776/ https://lwn.net/Articles/825776/ renox <div class="FormattedComment"> I wonder why everyone just doesn&#x27;t copy DLang solution: using !( ) for generics: no ambiguity, no need to hack the parser, no conflict with with arrays.<br> <p> <p> <p> </div> Thu, 09 Jul 2020 17:10:24 +0000 Generics for Go https://lwn.net/Articles/825635/ https://lwn.net/Articles/825635/ briangordon So I guess the suggested usage is still to use interface{} when there are no type constraints... <pre>func Keys(type K comparable, V interface{})(m map[K]V) []K {</pre> That being said I do think the choice to forgo covariance and contravariance in favor of simply talking of type constraints as interfaces is very golang. Not exactly the language I would prefer to use but it's at least consistent. Wed, 08 Jul 2020 21:34:29 +0000 Generics for Go https://lwn.net/Articles/825543/ https://lwn.net/Articles/825543/ mathstuf <div class="FormattedComment"> <font class="QuotedText">&gt; but you&#x27;re already going to have to do kind-checking there anyway (i.e. to prohibit Int&lt;List&gt; and similar errors), so additionally checking that the argument *is* a type isn&#x27;t really that much more work.</font><br> <p> But you don&#x27;t want your AST module to have to decide that. It feels like a layering violation to me.<br> </div> Wed, 08 Jul 2020 13:41:31 +0000 Generics for Go https://lwn.net/Articles/825528/ https://lwn.net/Articles/825528/ ncm <div class="FormattedComment"> C++ generics do not, in fact, draw on Ada generics, and the Ada attempt at an STL was a dismal failure, as it would also be in Java were it attempted.<br> <p> C++ generics do owe a lot to ML (not &quot;machine learning&quot;; there was a language ML that also influenced Haskell). Java generics are extremely limited, and their backward compatibility means they offer no performance improvement over older mechanisms.<br> </div> Wed, 08 Jul 2020 06:58:38 +0000 Generics for Go https://lwn.net/Articles/825518/ https://lwn.net/Articles/825518/ NYKevin <div class="FormattedComment"> <font class="QuotedText">&gt; When parsing code within a function, such as v := F&lt;T&gt;, at the point of seeing the &lt; it&#x27;s ambiguous whether we are seeing a type instantiation or an expression using the &lt; operator. Resolving that requires effectively unbounded lookahead. In general we strive to keep the Go parser efficient.</font><br> <p> Eh... Python used square brackets to solve this exact problem (which had the bonus of being entirely backwards-compatible with their existing syntax; Python&#x27;s types are &quot;just&quot; variables pointing to class objects, so applying the subscript operator was already syntactically legal). Perhaps Go didn&#x27;t want to parse generics as an extension/generalization of the subscript operator? I suppose that saves you the trouble of disambiguating generics from &quot;classical&quot; subscripting at the semantics layer, but you&#x27;re already going to have to do kind-checking there anyway (i.e. to prohibit Int&lt;List&gt; and similar errors), so additionally checking that the argument *is* a type isn&#x27;t really that much more work.<br> <p> Maybe they just thought that parentheses were less ugly than square brackets?<br> </div> Tue, 07 Jul 2020 20:49:46 +0000 Generics parameter syntax https://lwn.net/Articles/825437/ https://lwn.net/Articles/825437/ dtlin It helps for that case, but if you have higher-order kinds, <tt>f&lt;t&gt;</tt> is also a type, for some appropriate type variables <tt>f</tt> and <tt>t</tt>. Tue, 07 Jul 2020 12:19:23 +0000 Generics parameter syntax https://lwn.net/Articles/825421/ https://lwn.net/Articles/825421/ dezgeg <div class="FormattedComment"> Haskell was just an example of some other language where caseness of a name matters (but apparently even Go already uses it in other manner). I was thinking about having names starting with Uppercase always refer to types and names starting lowercase be variables. Thus in the v := F&lt;T&gt; example the caseness of F would tell whether the &lt; is the less than operator or the opening bracket of a type parameter.<br> </div> Mon, 06 Jul 2020 20:38:38 +0000 Generics parameter syntax https://lwn.net/Articles/825337/ https://lwn.net/Articles/825337/ dtlin <p>In Haskell, types do begin with an upper-case letter or <tt>:</tt> symbol, but data constructors (which are values) also begin with an upper-case letter or <tt>:</tt> symbol and type variables begin with a lower-case letter or non-<tt>:</tt> symbol, so that's not useful for disambiguation. <p>But only values appear to the left of <tt>::</tt> and only types appear to the right of <tt>::</tt>, so there's no confusion. Except in import lists, or when <tt>-XTypeApplications</tt>, <tt>-XDataKinds</tt>, <tt>-XStarIsType</tt>, … No angle brackets so it doesn't suffer from C++'s problems though. Sun, 05 Jul 2020 22:23:28 +0000 Generics parameter syntax https://lwn.net/Articles/825327/ https://lwn.net/Articles/825327/ Cyberax <div class="FormattedComment"> Go uses upper case names for exported symbols and lower-case names for package-local ones.<br> </div> Sun, 05 Jul 2020 19:11:53 +0000 Generics parameter syntax https://lwn.net/Articles/825301/ https://lwn.net/Articles/825301/ dezgeg <div class="FormattedComment"> In some languages (Haskell comes to mind, also Erlang I think) case-sensitivity is used to enforce this kind of problems without requiring a symbol table in the parser. In Haskell, uppercase names always refer to types or type constructors; and lowercase names always refer to variables or type parameters. Sadly not many new languages seem to take use of this as I personally find it quite elegant. <br> </div> Sun, 05 Jul 2020 10:06:23 +0000 Generics parameter syntax https://lwn.net/Articles/825295/ https://lwn.net/Articles/825295/ mathstuf <div class="FormattedComment"> Note that I actually don&#x27;t know how Go&#x27;s parser in particular works. I surmise that the parser requiring access to a symbol table would be unacceptable for performance. It&#x27;d be better to let the parser go and make ASTs for all the source and only then figure out what symbols are defined where (using the imports as a guideline). In fact, the mere existence of the AST module in the standard library likely means that symbol tables cannot be allowed (because requiring `import` to actually do work and look at what symbols are available just to parse `sym&lt;arg&gt;` would be a little ridiculous IMO).<br> </div> Sun, 05 Jul 2020 02:26:46 +0000 Generics for Go https://lwn.net/Articles/825267/ https://lwn.net/Articles/825267/ cpitrat <div class="FormattedComment"> &quot;(The C approach.) Leave them out. This slows programmers. But it adds no complexity to the language.&quot;<br> <p> Actually the C approach is drop the safety net (typing) and use (void *)<br> <p> &quot;The generic dilemma is this: do you want slow programmers, slow compilers and bloated binaries, or slow execution times?&quot;<br> <p> Actually the first option&#x27;s issue is not slow development, it&#x27;s choosing between loosing type safety or duplicating code which in both cases, means higher risk of bugs. Yes it also means slower development, but not by a huge amount and that&#x27;s not a big issue.<br> </div> Sat, 04 Jul 2020 08:26:55 +0000 Generics for Go https://lwn.net/Articles/825266/ https://lwn.net/Articles/825266/ cpitrat <div class="FormattedComment"> IIUC, this is conceptually like the C++ approach but with a nicer syntax (same as the language rather than a dedicated one). AmI missing something?<br> </div> Sat, 04 Jul 2020 08:19:33 +0000 Generics parameter syntax https://lwn.net/Articles/825246/ https://lwn.net/Articles/825246/ davecb <div class="FormattedComment"> Thanks: I come from an era when we assumed a symbol table, and so did not realize Go does not have one in it&#x27;s parser. Rather amazing!<br> </div> Fri, 03 Jul 2020 22:39:24 +0000 Generics parameter syntax https://lwn.net/Articles/825237/ https://lwn.net/Articles/825237/ mathstuf <div class="FormattedComment"> How complicated can `T` get syntactically? Can you decide before you see the `&gt;` somehow? This is the reason for the weird `obj.mem1.mem2.template foo&lt;T&gt;` syntax in C++: T can be very complicated and &quot;type or expression&quot; is not something one can answer without actually having a name lookup table.<br> </div> Fri, 03 Jul 2020 18:49:22 +0000 Generics parameter syntax https://lwn.net/Articles/825111/ https://lwn.net/Articles/825111/ davecb <div class="FormattedComment"> I rather wonder if v := F&lt;T&gt; can be parsed both accurately and efficiently with strongly bounded lookahead? <br> </div> Thu, 02 Jul 2020 15:34:31 +0000 Generics for Go https://lwn.net/Articles/825042/ https://lwn.net/Articles/825042/ ragnar <div class="FormattedComment"> I really like the approach zig has taken with generics. They simply have types as compile-time parameters to functions and data structures.<br> <p> <a href="https://ziglang.org/#Generic-data-structures-and-functions">https://ziglang.org/#Generic-data-structures-and-functions</a><br> </div> Thu, 02 Jul 2020 09:32:23 +0000 Generics for Go https://lwn.net/Articles/825035/ https://lwn.net/Articles/825035/ k3ninho <div class="FormattedComment"> <font class="QuotedText">&gt;Not that I know much about Go</font><br> Read the &quot;Featherweight Go&quot; journal article [1] and its parent, &quot;Featherweight Java&quot;[2]. I think Rob Pike looked and though Featherweight Java more relevant because of the user-facing side of Go is more like the C++/C#/Java kind of object orientation than the approach taken by Ada. You should be heartened that both Featherweight approaches prove (rather than merely providing) type safety.<br> <p> K3n.<br> <p> 1: <a href="https://arxiv.org/pdf/2005.11710.pdf">https://arxiv.org/pdf/2005.11710.pdf</a><br> 2: <a href="https://dl.acm.org/doi/10.1145/503502.503505">https://dl.acm.org/doi/10.1145/503502.503505</a><br> </div> Thu, 02 Jul 2020 08:25:35 +0000 Generics for Go https://lwn.net/Articles/824993/ https://lwn.net/Articles/824993/ dvdeug <div class="FormattedComment"> How is this related? Go seems like a decidedly different language for a decidedly different audience. What&#x27;s the expected win from native compilation? <a href="https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/go-csharpcore.html">https://benchmarksgame-team.pages.debian.net/benchmarksga...</a> says that C# already ties or beats Go on every single problem.<br> </div> Wed, 01 Jul 2020 20:27:46 +0000 Generics for Go https://lwn.net/Articles/824995/ https://lwn.net/Articles/824995/ Cyberax <div class="FormattedComment"> <font class="QuotedText">&gt; Though surely Java too, these days, lets you create a vector of ints without having to box each one?</font><br> Nope. That&#x27;s on the TODO list, though: <a href="https://openjdk.java.net/projects/valhalla/">https://openjdk.java.net/projects/valhalla/</a><br> </div> Wed, 01 Jul 2020 20:17:18 +0000 Generics for Go https://lwn.net/Articles/824994/ https://lwn.net/Articles/824994/ epa <div class="FormattedComment"> Yes, I was surprised not to see C# mentioned in the discussion, since it seems to avoid the macro expansion approach of C++, while not suffering the boxing overhead of Java. (Though surely Java too, these days, lets you create a vector of ints without having to box each one?)<br> </div> Wed, 01 Jul 2020 20:15:40 +0000 Generics for Go https://lwn.net/Articles/824988/ https://lwn.net/Articles/824988/ dvdeug <div class="FormattedComment"> C++ templates were designed after Ada generics, and Alexander Stepanov, who wrote the Standard Template Library for C++ and pushed for C++ templates with Bjarne Stroustrup, did his first proto-STL in Ada. So C++ and Java should show a more refined form of Ada generics.<br> <p> Personally, I&#x27;ve worked with Ada generics. The Ada type system added complexities in places that don&#x27;t seem to have wins in retrospect, and Ada generics reflect that. They don&#x27;t have the sheer power that C++ templates have, or the usual elegance of Java. I&#x27;d be interested to see languages from a universe where Ada was more successful and modern languages stuck more to its design principles, but in any case, Ada generics were the first major generics implementation, and it shows.<br> </div> Wed, 01 Jul 2020 19:53:19 +0000 Generics for Go https://lwn.net/Articles/824985/ https://lwn.net/Articles/824985/ acarno <div class="FormattedComment"> Not that I know much about Go, but why not look at generics in languages other than C/C++/Java? For example, Ada has (strongly-typed) generics:<br> <a href="https://www.adaic.org/resources/add_content/standards/05rm/html/RM-12.html">https://www.adaic.org/resources/add_content/standards/05r...</a><br> </div> Wed, 01 Jul 2020 19:13:04 +0000 Generics for Go https://lwn.net/Articles/824976/ https://lwn.net/Articles/824976/ kjp <div class="FormattedComment"> If only google and msft could team up and get native compilation going for C#. Nope, gotta be NIH.<br> </div> Wed, 01 Jul 2020 17:21:35 +0000 Generics for Go https://lwn.net/Articles/824971/ https://lwn.net/Articles/824971/ Cyberax <div class="FormattedComment"> I&#x27;m honestly amazed by Ian Lance Taylor. He&#x27;s active in most of discussions about generics and always takes time to calmly discuss questions and explain issues. <br> </div> Wed, 01 Jul 2020 16:44:35 +0000