Live data from Hacker News

A Proposal for Adding Generics to Go

blog.golang.org

231–240 of 273 posts

Re: A Proposal for Adding Generics to Go

#231

Earlier quoted context omitted.

I thought the idomatic approach was to represent sets with map[key]bool

As someone who finds "indicating intent in the code" an important thing, I must admit I find this concert slightly horrifying. A Map and a Set are two different things and which one you use conveys some intent as to what you mean by your code. I get that it works, but it would still make me unhappy to do.

> A Map [with value type = void/unit/() [0]] and a Set are two different things

Not to defend Go or anything, but that's like saying:

| A Array [with element type = byte/char/u8] and a String are different things

It might be useful to call them different names (of course that would require Go to support generic typedefs for `type Set k = Map k Void`), but they're still fundamentally the same thing.

0: which, to be fair, is not the same thing as a map with bool values.

Re: A Proposal for Adding Generics to Go

#232

Earlier quoted context omitted.

Yeah that's ok until you have 20 million lines of generic ridden crapola pumped out by the lowest bidder. That's the hell I spent a good chunk of the last few years untangling on the C# front. Let's model this correctly! Oh no someone said fuck it, lets just use a bunch of generic data types! Dictionary >>, SortedSet >>> Several thousand out of bounds, missing keys, null reference exceptions, hash collisions and the…

Agreed. I'll be banning generics from any code I have control over unless there's a very good reason for it. I saw too much of this crap in C#, and ran from it screaming.

Say you need a binary heap to hold some customer records, and another binary heap to store some orders. How many times do you implement a binary heap?

Re: A Proposal for Adding Generics to Go

#233

Earlier quoted context omitted.

Yeah that's ok until you have 20 million lines of generic ridden crapola pumped out by the lowest bidder. That's the hell I spent a good chunk of the last few years untangling on the C# front. Let's model this correctly! Oh no someone said fuck it, lets just use a bunch of generic data types! Dictionary >>, SortedSet >>> Several thousand out of bounds, missing keys, null reference exceptions, hash collisions and the…

`Dictionary >>, SortedSet >>>` This is not a problem with generics, but with C#'s lack of discriminated unions and/or tiny-types. Except what on earth are you doing with a dictionary whose keys are lists of dictionaries? I am quite sure someone has not modelled their domain correctly there. That's not something you can blame on the existence of generics - I shudder to imagine how much worse it could have been without…

> I shudder to imagine how much worse it could have been without generics!

It probably would have just become a very long String pretty early on. It would have xml in it, but not always, because nobody is that lucky.

Re: A Proposal for Adding Generics to Go

#235

Earlier quoted context omitted.

I actually just need generic Sets. Generic map/reduce on slices wouldn't hurt too. OTOH, it's 2021 and look at what we are wishing. My love/hate relationship with golang is like the one I have with Apple.

isn't generic Sets easily implemented with map being already generic?

For some uses of a set, yeah. For intersections / unions etc. nope. Then it's back to for loops, so many for loops in Go.

Re: A Proposal for Adding Generics to Go

#236
post #61
post #48

Earlier quoted context omitted.

Mostly concerned about having to read other people's code that uses it.

If others people code uses it, then those other people deemed it useful. So the argument for not having them now becames either: (a) they rather not have it available, because you personally don't find it useful (b) those using generic don't know what they're doing, and only people not using generic are smart, so it's better to not have them to prevent the clueless from being able to use them

(c) Some people using generics don't know what they are doing and would be much better off not doing so.

Re: A Proposal for Adding Generics to Go

#237

Earlier quoted context omitted.

I think map[T]bool is already a pretty good set; the only things you can do with sets are insertion, deletion, iteration and checking for existence and they're all well-supported. Of course, if you need a concurrent set you're right back in type system hell.

You can’t write intersection, union, difference, subset (contains all), or powerset as reusable functions for any element type. The idiomatic thing for now is to rewrite them as loops over and over, but that’s error prone, hard to read, and not a good use of time.

This was the exact issue I hit. There are set libraries for Go, but they only handle standard types. The for loops got old quick.

Re: A Proposal for Adding Generics to Go

#238
post #224

Earlier quoted context omitted.

Once upon a time, C was a general purpose programming language--it wasn't always exclusively performance critical systems programming. Anyway, in practice you can use arenas or other techniques to alleviate GC pressure. In my experience, the GC isn't a big performance issue; rather, the Go compiler doesn't optimize as aggressively as the C compiler.

Unless you need the precise memory and hardware control that C gives you, why lower your productivity by using such a low-level language?

Back in the day it was simply the best option available. Java and Python only came out in the mid-90s and would take some time to catch on, develop a useful ecosystem, etc. And if you wanted to interact with system APIs, everything was in C. Nowadays there are much better options for a huge swath of applications. I would argue that--every bit as important as the language issues itself--one of the biggest reasons to avoid C is that all of its popular build system options are terrible. You're just expected to have the right dependencies installed on your system at the right versions and in the right locations (okay, some tools will try and find the location for various dependencies, but this is a pretty poor substitute for proper dependency management). And that's merely scratching the surface of the issues with C/C++ build tooling.

Re: A Proposal for Adding Generics to Go

#239

Earlier quoted context omitted.

C doesn’t lack safety by-design - it’s hobbled by its history and constraints imposed by its userbase - otherwise C would have major breaking changes more often.

C absolutely lacks type-safety by design. Otherwise what would malloc return, besides void*? How would you implement generic containers in C, without using macros or void*? Edit: formatting

> Otherwise what would malloc return, besides void*

IIRC, historically void* didn't exist, and it used char*. It added void as a unit type, and absent historical baggage could add... let's call it "noreturn" as a bottom type:

  #define NULL ((noreturn*)0)
  noreturn* malloc(size_t);
  void free(void*);
  noreturn exit(int); // never returns
  int main() {
    // where (T)... is any expression of type T
    void x = (T)...; // throw away the value
    T y = (noreturn)...; // a nonterminating expression
    void* p = (T*)...; // pointers convertible *to* void*
    T* q = (noreturn*)...; // pointers convertible *from* noreturn*
    *(noreturn*)...; // notionally, this should always fault
    *(void*)...; // read zero bytes, so always fine
    }
Post reply on HN