Golang proposal: container/: generic collection types

(github.com)

74 points | by jabits 2 hours ago

6 comments

  • nasretdinov 44 minutes ago
    Well, better late than never. Stuff like sets or a typed heap is long overdue. Maybe they'll even add iterator API for database/sql results this decade too (something like my pull request for sqlx https://github.com/jmoiron/sqlx/pull/990 but maybe more polished)
    • nasretdinov 42 minutes ago
      Also having stuff like a concurrency limiter (instead of doing weird var limiter chan struct{} and using it as `limiter <- struct{}{}` and `defer <-limiter`) as a library function in `sync` package would be great too.
      • aatd86 24 minutes ago
        But I guess the issue is that oftentimes, it is more of a distributed system coordination issue?

        What use case do you have in mind?

  • jiehong 11 minutes ago
    Well, finally!

    I wish they wouldn’t mix mutation methods in there, but ok.

  • athorax 1 hour ago
    On one hand I'm glad to see this being added, but its becoming obvious building generics into the language as-is just isn't a good fit. Hopefully Go v2 can solve this at a more foundational level while still allowing interop or easy porting form current go code.
    • nasretdinov 37 minutes ago
      Why isn't it a good fit? The design prioritises readability of code that uses generics vs writing generic code (which is in line with the overall language philosophy). It also does it in a way that doesn't impact compilation speed or add too much complexity, which is also in line with the language philosophy.

      If you don't like the fact that not all of standard library has been refactored to support the new language features -- I think it'll come over time. Once these features land into the standard library they can't be taken away, so the language authors take their time to make sure it's designed well.

      I don't see anything going obviously wrong here.

    • bananamogul 45 minutes ago
      I thought the status of Go v2 was that the devs weren't saying it will never happen, but also were not working on it. There was some momentum that seemed to sputter out once generics were added.

      "when should we expect the Go 2 specification that breaks old Go 1 programs?

      The answer is never. Go 2, in the sense of breaking with the past and no longer compiling old programs, is never going to happen. Go 2 in the sense of being the major revision of Go 1 we started toward in 2017 has already happened."[1]

      Granted, this post was more about if there will someday be a Go that will break backward compatibility. But it sort of answers where Go 2 is as a side effect.

      [1] https://golang.google.cn/blog/compat

    • orf 45 minutes ago
      Why is it becoming obvious?
  • troupo 1 hour ago
    Go is rediscovering Guy Steele's Growing a Language from first principles, at a much slower pace: https://youtu.be/_ahvzDzKdB0?is=qZdfiT3792XyHxSJ
    • silverwind 1 hour ago
      It's sad that such basic stuff took this long. If we're lucky we might even see a `Map` function in our lifetimes.
      • PrimalPower 56 minutes ago
        The beauty of go is YAGNI. Language design makes it much harder for people so do stupid and cute things.

        During my design process if I start realizing that I’m missing maps, More expressive Types, Or more complex polymorphism. I ask myself if I really need those things.

        If I really do. I move off of go.

        That’s the beauty of the language. Go does not need more complicated language features because it’s can handle the majority of trivial software work without unnecessary complexity.

        The language does not need to solve complicated problems.

        • brokencode 3 minutes ago
          Ok, and what if you realize you want these things a million lines in? Do you still move off of Go?

          Generic programming isn’t some fancy research language feature like dependent types. It’s just a bare minimum feature in any modern typed language.

          It’s perplexing that after C# and Java both notably shipped without generics initially then added them later that they decided to ship Go without generics.. only to end up adding them later.

      • wannabe44 1 hour ago
        Map function leads to poor code in Go. Function literals are verbose and inlining is far less agressive. It simply isn't the way of the language. Even python shuns map and filter in favor of comprehensions. A for loop is more readable than the lambda soup.
    • pstuart 1 hour ago
      Sounds like a win to me. I'd assume that many PL decisions come with with costs of either implementation or readability, and Go's conservative evolution takes that into account. Disclaimer: not a PL designer, just a codemonkey.

      It's not a perfect language, and the process has not been without its pain points, but work like this makes the language more powerful and I feel like I get my money's worth when I use it.

      Yes, I'm a Go fanboy but I'm not interested in language wars (e.g., yes Rust is more performant and correct but sometimes "good enough" is good enough).

      • cyphar 1 hour ago
        On the other hand, design decisions made under one set of constraints can become problematic when you add new features that don't play as well with earlier design decisions.

        For a concrete example, the fact you cannot define custom methods for external types in Go (a pre-1.0 design decision) means that you cannot write proper compile-time generic code to deal with some generic wrapping type (dumb example -- getting a sum of the perimeters of a generic set of shapes in an externally-defined collection type) -- you are forced to work around it with runtime type-switching. There are all sorts of arguments you can make about simplicity but this is an objective shortcoming of Go that is directly caused by generics being added to Go long post 1.0.

        My take on this is that despite selfishly wanting more from Go for many years myself, adding more stuff to Go at this late stage is just slowly chipping away at Go's uniqueness with features that make a large number of people using them unhappy. (For example, while they make some APIs nicer, iterators turn a basic logic bug that is impossible with for loops -- forgetting to stop iterating when the loop has a "break" -- into a runtime panic. And they really suck to compose.)

        • aatd86 17 minutes ago
          __For a concrete example, the fact you cannot define custom methods for external types in Go (a pre-1.0 design decision) means that you cannot write proper compile-time generic code to deal with some generic wrapping type (dumb example -- getting a sum of the perimeters of a generic set of shapes in an externally-defined collection type) -- you are forced to work around it with runtime type-switching. There are all sorts of arguments you can make about simplicity but this is an objective shortcoming of Go that is directly caused by generics being added to Go long post 1.0.__

          I'm a bit intrigued by this: how would that work with compatibility between libraries, separate compilation and what not? My first instinct is that it would not be a good idea but I might be missing something..?

        • pstuart 54 minutes ago
          Fair enough. Primeagen did a video recently renouncing his love of Go over the concerns you've raised.

          That said, I'm assuming much of this "wobbly" functionality will live in libraries and will not be common in day to day coding (much as I've seen with generics so far). It's all optional after all.

          I use Go because it's simple, capable, and been my prime language for over a decade and that skill enables me to be valuable enough for a decently paying job.

          If I were younger with more energy and time on my hands I'd likely be a rustacean but I think I can ride out my career on this. YMMV.

          • cyphar 43 minutes ago
            I've maintained one of the major container runtimes (which is written in Go) for over a decade as well, I think I have a less rosy view of Go than you but I still think it's a nice language all things considered and still use it for some projects.
            • pstuart 20 minutes ago
              My goal is to be pragmatic about the issue (and everything else).

              When I started with it I saw it as the love-child of C and python. Then saw it as a successor of Java.

              I recognize that Rust will win this contest (although my understanding is that async still has sharp edges to round down). I'm old and tired and have lost the energy and focus for exploring new languages and believe that despite its shortcomings it will continue to have enough value to be useful.

              Good enough will do for me. I tip my hat to the all the other PL contenders and wish them all well (except Java -- PTSD from that destroyed any love I had for the language).

      • tikhonj 50 minutes ago
        "Everything is a trade-off" assumes you're on the Pareto frontier.

        Go is... very much not on the Pareto frontier of programming language design.

        • fragmede 14 minutes ago
          Somewhat. The trade off is it can be made more complex at the cost of its simplicity. You can argue that some of those tradeoffs don't in fact have to be made because it's not on the Pareto frontier, eg generics, but we'd have to have a specific discussion about specific language features instead of vague generic discission about the language.
        • pstuart 15 minutes ago
          I find language wars tiresome at this point. There was no claim that it was a frontier language, or even that it was a great language. My point was that the Go authors do try to consider the impact of enhancements to the language, which I think is appreciated by many that use it.
  • znpy 35 minutes ago
    Looking forward to see G2EE being released as a specification /s