Live data from Hacker News

Generic interfaces

go.dev

31–40 of 78 posts

Re: Generic interfaces

#31
post #14

I find C++ templates simpler than Go generics. With C++, you can at-least get to a design solution. With Go generics: oops this is not possible, oops that is not possible - all because of strange language limitations. Go's Generics are a crippled implementation - they don't really deserve the feature title of 'generics'. (Its like saying you support regex, but don't support groups and repeat operators and you can onl…

I don't disagree that Go's generics are pretty limited. But I find it a strange complaint, when contrasted with C++ templates. Which, as I understand, are literally not part of the type system and thus there seems to be a far stronger case, that they can not be called generics. The main difference between Go's generics and C++ templates (and where some of the restrictions come from) is that Go insists that you can ty…

C++ templates are duck typed at compile time.

Look at Haskell type classes or Rust's traits for some classic examples of how to 'type' your generics. (And compare to what Go and C++ are doing.)

Re: Generic interfaces

#32
post #7

If I'm being honest, the magic of Go was lost when generics were introduced. It now feels akin to Java, which I guess was inevitable and for anyone to really take it seriously maybe it needed to get here. But I am not a fan of generics. While that level of abstraction and composability is clever, it also lends itself to more complexity and systems that can be harder to concretely understand. Just an opinion that I kn…

> If I'm being honest, the magic of Go was lost when generics were introduced. It now feels akin to Java, [...]

Funnily enough, Java didn't use to have generics. I wonder whether it didn't feel like Java back then?

Re: Generic interfaces

#33

When I first learned about Go I thought the idea was to have a simple C-like language with a frozen feature set. A language that would look the same today and ten years from now. And I thought that boringness was a wonderful feature, actually. If they're going to be adding features to the language, albeit at a slower pace than Java/C#, what's the point really? On a long enough timeline Go is going to be indistinguish…

> When I first learned about Go I thought the idea was to have a simple C-like language with a frozen feature set.

C is a C-like language with a mostly frozen feature set. (If you want something less insane than C, there's also Pascal.)

Re: Generic interfaces

#34

“Maybe generics like in java and C# actually made sense after all.“ Welcome to civilization, golang. Were there ever any language developers with more hybris?

I think of it less as hybris and more of a (failed) experiment. It was deliberately built as a 'stupid' language for fresh undergrads, lacking design experience. It is a technical solution for a people problem. It is better to guide and to mentor people in designing the right abstractions. What we should learn from this experiment is that this is the wrong approach.

> It is a technical solution for a people problem. It is better to guide and to mentor people in designing the right abstractions. What we should learn from this experiment is that this is the wrong approach.

Nah, it was just the wrong solution.

People problems are basically intractable in the grand scheme of things. Whenever you can turn a people problem into a technical problem, that's an opportunity for progress.

Imagine telling everyone to be a professional and being careful not to break our program when they edit the code? Sounds like a big people problem!

Instead, we give everyone their own copy to muck around with (instead of a shared folder), and we only allow changes to be integrated into the 'master copy', if they pass automated tests.

A good manager and really motivated and professional workers can help cope with people problems. But there's a limit to their ability. So the more we can offload to technological solutions, the more 'professionalism' (for lack of a better word) we can spare for other task that aren't feasible to be solved via technology, yet.

And I agree that not all technical solutions work! You need to experiment, and make judgement calls.

Re: Generic interfaces

#37
post #31

Earlier quoted context omitted.

I don't disagree that Go's generics are pretty limited. But I find it a strange complaint, when contrasted with C++ templates. Which, as I understand, are literally not part of the type system and thus there seems to be a far stronger case, that they can not be called generics. The main difference between Go's generics and C++ templates (and where some of the restrictions come from) is that Go insists that you can ty…

C++ templates are duck typed at compile time. Look at Haskell type classes or Rust's traits for some classic examples of how to 'type' your generics. (And compare to what Go and C++ are doing.)

> C++ templates are duck typed at compile time.

"Compile time" is not the right distinction. This is about "instantiation time". Go's implementation specifically allows to type-check the body and the call separately. That is, if you import a third-party package and call a generic function, all the type checker needs to look at to prove correctness is the signature of the function. It can ignore the body.

This is especially relevant, if you call a generic function from a generic function. For C++, proving that such a call is correct is, in general, NP-complete (it directly maps to the SAT problem, you need to prove that every solution to one arbitrary boolean formula satisfies a different boolean formula). So the designers made the conscious decision to just not do that, instead delaying that check to the point at which the concrete type used to instantiate the generic function is known (because checking that a specific assignment satisfies a boolean formula is trivial). But that also means that you have to (recursively) type-check a generic function again and again for every type argument provided, which can drive up compilation time.

A demonstration is this program, which makes gcc consume functionally infinite amount of memory and time: https://godbolt.org/z/crK89TW9G (clang is a little bit more clever, but can ultimately be defeated using a similar mechanism).

Avoiding these problems is a specific cause for a lot of the limitations with Go's generics.

> Look at Haskell type classes or Rust's traits for some classic examples of how to 'type' your generics. (And compare to what Go and C++ are doing.)

Yes, those are a different beasts altogether and the differences between what Go is doing and what Haskell and Rust are doing requires different explanations.

Though it's illustrative, because it turns out Rust also intentionally limited their generics implementation, to solve the kinds of performance problems Go is worried about. Specifically, Rust has the concept of "Dyn compatibility" (formerly "Object safety") which exists because otherwise Rusts goal of zero-cost abstractions would be broken. Haskell doesn't have this problem and will happily allow you to use the less efficient but more powerful types.

(All of this should have the caveat that I'm not an expert in or even a user of any of these languages. It's half-knowledge and I might be wrong or things might have changed since I last looked)

Re: Generic interfaces

#38

Remember when Go proposed as a simple language? What a shitshow. Seems like Go's designers didn't know about interfaces, generics, and iterators when decided to make a language...

Market for simple language opened. Someone will fill it. Go failed expectations, by turning into C++.

Re: Generic interfaces

#39
Preaching to the choir here, but this is why a lot of the Go community was against generics.

Especially in the era of AI assistants, the downside of writing out explicit types and repetition matters very little, while the upside of avoiding all this complexity is unmeasurable.

Re: Generic interfaces

#40
post #19
post #15

Sort of wild that the Go blog doesn't have Go syntax highlighting...

It makes more sense if you know about Rob Pike: https://groups.google.com/g/golang-nuts/c/hJHCAaiL0so/m/kG3B... >Syntax highlighting is juvenile. When I was a child, I was taught arithmetic using colored rods ( http://en.wikipedia.org/wiki/Cuisenaire_rods ). I grew up and today I use monochromatic numerals. The language creator really hates it (and most modern editor tooling).

Smugness 101, or how to convey a personal preference in the most insufferable way imaginable.
Post reply on HN