Live data from Hacker News

My real life use-case for generics in Go: API for client-side pagination

vladimir.varank.in

11–20 of 30 posts

Re: My real life use-case for generics in Go: API for client-side pagination

#11

I am surpsised by the fact that in 2022 go developers are rediscovering and being fascinated by generics when any general introductory programming education with a type safe language should have exposed them to it. It feels like a massive step back in what you should expect the average developer to know about programming.

Anyone can learn how to write generic code, the trick is knowing the price of the particular implementation of generics and when to pay it.

https://planetscale.com/blog/generics-can-make-your-go-code-...

Re: My real life use-case for generics in Go: API for client-side pagination

#12
post #10

Earlier quoted context omitted.

Do you know how Go handled collection classes in the past without generics? Where they just not typesafe, like a Java List ?

Built-in map and vector types are generic. E.g. map[int]*MyType Using interface{} is quite common in the absence of sum types (or inheritance with base class). I guess interface{} is like Object in Java. It's not worse than Python or other dynamic languages that many people are fine with. The run-time panics when asserting that a type is something it's not. E.g. https://go.dev/play/p/c4hx8HSiB8I That said, no, it's n…

>Built-in map and vector types are generic.

I understand what people mean by this, but it's not true for any reasonable notion of 'generic'. Pre-generics Go no more has generic maps than C has generic arrays. Yes, you can declare a map or a slice of values of any given type, but you can't write a generic function that abstracts over these types. That's what is usually meant by 'generics'.

Re: My real life use-case for generics in Go: API for client-side pagination

#13
post #3

Earlier quoted context omitted.

Without being too flippant, that is Go’s history in a nutshell.

Do you know how Go handled collection classes in the past without generics? Where they just not typesafe, like a Java List ?

The usual pattern was to use an empty interface (`var collection []interface{}`).

Re: My real life use-case for generics in Go: API for client-side pagination

#14
post #8

I am surpsised by the fact that in 2022 go developers are rediscovering and being fascinated by generics when any general introductory programming education with a type safe language should have exposed them to it. It feels like a massive step back in what you should expect the average developer to know about programming.

I’m sure that you can find similar articles for Haskell “My real world use-case for loops”. There’s a certain kind of programmer who delights in using languages that have something missing, for puristic reasons or the challenge[0]. And sometimes these languages develop religious dogma: not only can you program without loops: it’s better for you too! Like a fad diet. These diet languages attract other people who perha…

Linux ecosystem and C

cough

Re: My real life use-case for generics in Go: API for client-side pagination

#15
post #8

I am surpsised by the fact that in 2022 go developers are rediscovering and being fascinated by generics when any general introductory programming education with a type safe language should have exposed them to it. It feels like a massive step back in what you should expect the average developer to know about programming.

I’m sure that you can find similar articles for Haskell “My real world use-case for loops”. There’s a certain kind of programmer who delights in using languages that have something missing, for puristic reasons or the challenge[0]. And sometimes these languages develop religious dogma: not only can you program without loops: it’s better for you too! Like a fad diet. These diet languages attract other people who perha…

This is such a biased view. Java doesn't have functions and you have to work around that (by treating your classes as namespaces). Python doesn't have a type system and you have to deal with that (by using your brain, I guess). Sometimes it fits language design ("religious dogma" in the eyes of some), sometimes it simply didn't get implemented.

Loops in Haskell are pretty common, tho. Perhaps you meant concept like for loop and this doesn't really fit.

Re: My real life use-case for generics in Go: API for client-side pagination

#16

I am surpsised by the fact that in 2022 go developers are rediscovering and being fascinated by generics when any general introductory programming education with a type safe language should have exposed them to it. It feels like a massive step back in what you should expect the average developer to know about programming.

Anyone can learn how to write generic code, the trick is knowing the price of the particular implementation of generics and when to pay it. https://planetscale.com/blog/generics-can-make-your-go-code-...

I guarantee you that less than 1% of users of generic languages have the knowledge to argue about this.

Java devs are in a special position since all had to find out the hard way you can't acquire generic parameters at runtime due to type erasure. In Java generics are just syntactic sugar, but this gives them the lowest (none?) runtime overhead.

Re: My real life use-case for generics in Go: API for client-side pagination

#17

I am surpsised by the fact that in 2022 go developers are rediscovering and being fascinated by generics when any general introductory programming education with a type safe language should have exposed them to it. It feels like a massive step back in what you should expect the average developer to know about programming.

I majored in computer engineering and only touched embedded C and assembly (and MATLAB). I later picked up Go when I wanted to write code to run on regular computers. I never actually touched generics until they were being introduced to Go and I genuinely didn't realize what they could be used for.

Re: My real life use-case for generics in Go: API for client-side pagination

#18

I am surpsised by the fact that in 2022 go developers are rediscovering and being fascinated by generics when any general introductory programming education with a type safe language should have exposed them to it. It feels like a massive step back in what you should expect the average developer to know about programming.

I think think post isn’t about discovering or being fascinated by generics, but just demonstrating what they feel is a good use case. The general guidance I’ve heard (including from the Go team) is that generic code is not preferable and should be reserved for use cases that particularly benefit from it. Generics in Go are brand new so it is good to see many uses (and misuses) to help establish the best patterns.

Re: My real life use-case for generics in Go: API for client-side pagination

#19

I am surpsised by the fact that in 2022 go developers are rediscovering and being fascinated by generics when any general introductory programming education with a type safe language should have exposed them to it. It feels like a massive step back in what you should expect the average developer to know about programming.

This is a pretty bad take on the article. When Ruby added pattern matching, I was excited and interested in it because I had seen it in other languages and wanted to see how it could work in Ruby. This is no different.

Re: My real life use-case for generics in Go: API for client-side pagination

#20
post #12
post #10

Earlier quoted context omitted.

Built-in map and vector types are generic. E.g. map[int]*MyType Using interface{} is quite common in the absence of sum types (or inheritance with base class). I guess interface{} is like Object in Java. It's not worse than Python or other dynamic languages that many people are fine with. The run-time panics when asserting that a type is something it's not. E.g. https://go.dev/play/p/c4hx8HSiB8I That said, no, it's n…

>Built-in map and vector types are generic. I understand what people mean by this, but it's not true for any reasonable notion of 'generic'. Pre-generics Go no more has generic maps than C has generic arrays. Yes, you can declare a map or a slice of values of any given type, but you can't write a generic function that abstracts over these types. That's what is usually meant by 'generics'.

Sure but it's worth emphasizing that Go language designers gave these built-in collections the exorbitant privilege of being parameterized by a type and declaring that any new type created by a mere programmer has to be monomorphic.
Post reply on HN