Live data from Hacker News

Go generics are not bad

lemire.me

181–190 of 305 posts

Re: Go generics are not bad

#181
post #139

Earlier quoted context omitted.

Writing a generic function to sum an array of any numbers type is trivial in C++. There's no 4 day research period, even if you're new to templates. template T sum(T* x, size_t size) { T acc = 0; for(size_t i = 0; i

Isn't acc and the xs same width here? So you risk overflow?

Yes, obviously calculating the sum of fixed-width integers may result in overflow.

Re: Go generics are not bad

#182
post #88

Earlier quoted context omitted.

Generics are great when you need them, but they will cut you badly if you misuse them. Generics are viral. When you make something generic, you often have to make the things that touch or contain it generic, too. Generics also create tight coupling. When you change the definition of a generic interface/class, you'll need to update your usage across the codebase. As opposed to, say, adding a new field to a class, that…

Please show code then I dont remember misusing C# generics even once and I struggle to see example of such a case

The only example I can think of is the bifurcation non-generic collections vs. generic collections in the early days of C# when you had a non-generic collection and couldn't pass it to a method which expects a generic collection, but it was the consequence of the development history of C# and is not a problem of generics per se.

Re: Go generics are not bad

#183
post #88

Earlier quoted context omitted.

Generics are great when you need them, but they will cut you badly if you misuse them. Generics are viral. When you make something generic, you often have to make the things that touch or contain it generic, too. Generics also create tight coupling. When you change the definition of a generic interface/class, you'll need to update your usage across the codebase. As opposed to, say, adding a new field to a class, that…

>Generics are viral. When you make something generic, you often have to make the things that touch or contain it generic, too Do you have an example of that? Can't you always "typedef" any particular generic type as a concrete type and work with that going forward? >Generics also create tight coupling. When you change the definition of a generic interface/class, you'll need to update your usage across the codebase. A…

https://github.com/dotnet/csharplang/issues/1328

> Have you ever wanted an interface (or abstract type) to be parameterized on a polymorphic variable to its implementer, but not its users? Have you ever wanted to parameterize over internal state, without revealing what that state contains to your users? This is what existential types allow you to do.

Re: Go generics are not bad

#184
post #91

Earlier quoted context omitted.

This is not the case with traits/type class approach seen in Rust/Haskell.

I'm afraid that it most certainly is, as Rust is the only language I have ever used generics in, and I had this problem. Lifetimes suffer from the exact same problem as well, since they're really an exotic form of generic. That said, I will readily admit that it was a lack of skill on my part. From talking to people in the Rust community though, I gather this isn't an uncommon experience.

Lifetimes are viral in Rust because 1. you can't abstract over them, 2. affine types are viral by design, but generics themselves are anything but viral.

Re: Go generics are not bad

#185
post #178
post #158

Earlier quoted context omitted.

Go 's compilation speed is only impressive for newer generations that never used compilers for Pascal dialects, Modula languages, BASIC,....

True but they didn't have to deal with the complexity of the modern software where a typical project can pull in thousands of dependencies (transitively).

That is taken care by using binary libraries, see Delphi or Eiffel.

Re: Go generics are not bad

#186
post #91

Earlier quoted context omitted.

I'm afraid that it most certainly is, as Rust is the only language I have ever used generics in, and I had this problem. Lifetimes suffer from the exact same problem as well, since they're really an exotic form of generic. That said, I will readily admit that it was a lack of skill on my part. From talking to people in the Rust community though, I gather this isn't an uncommon experience.

They're not viral in rust - you can always replace the generic with a concrete type in super types.

Rust also has associated types, which are exposed to the implementer, but not to the consumer.

Re: Go generics are not bad

#187
post #163

Earlier quoted context omitted.

This requires an exception mechanism to handle allocation failure. This issue pervades C++ when exceptions are disabled. Important error handling goes unhandled.

How does Go handle allocation failure? In my understanding objects may be allocated on the stack or heap per the compiler's whims. If a heap allocation fails, you just get "fatal error: runtime: out of memory" and an abort.

Your understanding is correct. That's also what you will effectively get with C or C++ on Linux anywhere you would have had a choice between C++ and Go to behind with.

Re: Go generics are not bad

#188

Earlier quoted context omitted.

I’m pretty sure Go’s maintainers were aiming for “mere” practical success, as opposed to some theoretical success that only academic languages succeed at.

What's the definition of "success" here? Popularity?

I think so. I wish OCaml or Clojure had become popular like Go and Rust when they were the fresh thing of the day..

Re: Go generics are not bad

#189
post #156

Earlier quoted context omitted.

Can’t hate on generics, so we need to find something else. Go takes away type system fidget spinners and leave devs with nothing to do but the actual job, which they hate, and thus they hate Go.

No it's simpler than that, they hate Go cause it's a half-assed language. The creators didn't respect developers enough to make it consistent and complete cause "devs are not smart enough". The argument about "getting things done" is like saying the language is Turing complete.

No post body was provided.

Re: Go generics are not bad

#190
post #163

Earlier quoted context omitted.

How does Go handle allocation failure? In my understanding objects may be allocated on the stack or heap per the compiler's whims. If a heap allocation fails, you just get "fatal error: runtime: out of memory" and an abort.

Your understanding is correct. That's also what you will effectively get with C or C++ on Linux anywhere you would have had a choice between C++ and Go to behind with.

Since when do heap allocation failures on Linux result in an abort? In C, malloc(3) will return NULL on failure and set errno accordingly. Sure, if overcommit is enabled, you might get a fault if you try to access memory that was allegedly allocated, but there is no strict "malloc failure === fatal error" relationship.
Post reply on HN