top | item 29584564

(no title)

horsemans | 4 years ago

I have never needed generics in Go, and I've probably been using it since 2017. I've never even once had to resort to any interface{} trickery to express what I want, and I've written Go programs for Fortune 50 companies, as well as complex personal projects such as AST parsers/code generators.

I'm pretty disappointed to see generics introduced into the language and every example I've seen feels completely unreadable to me compared to pre-generics implementations.

To be clear, it has never been the case that the Golang authors were 100% against generics. It has always been their position that the implementation needed to be good enough to make the trade-offs worthwhile. I just don't think they chose the right trade-offs.

discuss

order

kodablah|4 years ago

> I've never even once had to resort to any interface{} trickery to express what I want, and I've written Go programs for Fortune 50 companies, as well as complex personal projects such as AST parsers/code generators.

That's pretty surprising to me. Have you never had to implement marshalers for unknown types and such? I have had to implement things like json.Marshal and json.Unmarshal for different encodings dozens of times in my Go tenure. I have had to use reflection a lot. I have had to deserialize into map[string]interface{} to handle ambiguous situations at runtime a lot. Have you never even had to wrap or build your own Printf equivalents that accept interface{}? No loggers? No custom containers? None of that which operates on unknown types?

I see use of interface{} all over the vast majority of Go projects. I think your experience may be atypical.

horsemans|4 years ago

It's entirely possible that, through some strange quirks of circumstance, I've managed to avoid every problem space that would make me wish for generics.

In spite of that, it's unlikely that I've written implementations where using interface{} would be easier to read and reason about than not using interface{}. And the experience of the author whose blog post we're commenting on tracks with mine: "In my 5+ years working in Go, I can probably count on one hand the number of times that I felt like I really needed generics." I can too, just without using any fingers :-)

chexx|4 years ago

> I have had to deserialize into map[string]interface{} to handle ambiguous situations at runtime a lot.

Something I worry about is if I'm getting too jaded. You really think things are different elsewhere, but then you see that we all eat the same shit sandwhich.

That code would never have to be written if someone just used their brain before switching their integer IDs to string GUIDs. Bless your soul but I wish we didn't have to resort to such things. Some things code can't fix.

flippinburgers|4 years ago

His experience coincides with my own having solved many business level problems using golang for several years.

At most I have used non-empty interfaces to solve very few number of issues (countable on one hand). I have never needed interface{}.

hnlmorg|4 years ago

> as well as complex personal projects such as AST parsers/code generators.

Funny you say that because for me that's the one use case I have for generics.

(edit: why the hell am I getting downvoted for posting a fact? I wasn't offensive, argumentative, etc. Just citing one example I've run into where generics would help me personally)

grey-area|4 years ago

There are areas where they will help, and be pretty transparent to the user, lots of places in the stdlib, one trivial example: math.Max(a,b) could take any numeric instead of just float64, sort could be neater etc.

Perhaps a Go 2 if they ever get there could be a generic std lib rewrite, largely transparent to the end user, with some minor incompatibilities allowed and lots of stuff rewritten behind the scenes. They could remove a few ugly corners in the stdlib naming specific types by using generics, add a few more container types perhaps, deprecate some old stuff and move it out.

hnlmorg|4 years ago

I'm hoping Go 2 replaces the file struct with an interface too. That's been a particular pain point for me.

qaq|4 years ago

never needed min/max ?

horsemans|4 years ago

Would you use generics to write the same implementation of min/max for integers and floats?