top | item 31416896

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

46 points| varankinv | 3 years ago |vladimir.varank.in

29 comments

order

wejick|3 years ago

Not the best case to use generic as I'm thinking of composing a common pager struct within each kind of data would do, but in a bit different ergonomics.

Traubenfuchs|3 years ago

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.

jerf|3 years ago

The thing that people who are agog about people actually liking Go should understand is "any general introductory programming education with a type safe language should have exposed them to it" is completely correct.

To the best of my knowledge, nobody is learning Go as their first and only language at school. Very few people are learning it as their only language, certainly not enough to account for its growth by any means.

Therefore: You have a model of the world. Your model of the world is making the busted prediction that Go shouldn't be useful for anything and nobody should like it. Instead of cutting to the assumption that "Oh, all the people who like Go must just be stupid and ignorant" to fix it up, maybe you should consider updating your model with something else. I doubt there's more than 5% of Go users who have literally never used a language with generics and are somehow unaware of them. It certainly doesn't cover me, I've used Haskell quite a bit and can easily find the generics of other languages quite lacking without trying too hard.

(One thing I can tell you is all y'all agog folk really underestimate just how many of the use cases for "generics" was covered by "interfaces". Quite a lot of the "generic" use cases that leap to mind that aren't data structures parameterized by types are covered by interfaces. Even some of the "data structures parameterized by types" use cases are covered by interfaces too, just with less compiler assistance. To my mind, Go didn't "get generics" in 1.18, it merely added in the remaining 25% or so. This isn't the whole story, but it's a fair bit of it.)

s-video|3 years ago

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.

LVB|3 years ago

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.

kitd|3 years ago

I don't think they're particularly surprised or fascinated by generics. Just glad that they have finally appeared after years in the planning.

tchaffee|3 years ago

> what you should expect the average developer to know

This sounds like unnecessary gatekeeping. By whose standards? Most of the web runs on JavaScript and PHP. With TypeScript the frontend is steadily moving towards more "type safety" (an ambiguous term). But the point is that most of the useful things on the web have been built using languages that take the opposite approach. And there is a good argument to be made that a dead simple learning curve (without having to learn about types for example) is how we ended up with so many webapps so quickly. Much of the early web was built by amateurs.

zarzavat|3 years ago

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 perhaps don’t have a broad experience with many paradigms and they believe everything that has been written about the evils of loops.

Finally someone jury-rigs loops back into Haskell. Behold! The unlooped slowly open their eyes and shift out of their slumber. “It turns out loops are actually useful, huh.”

[0] http://prog21.dadgum.com/38.html

vlunkr|3 years ago

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.

vsnf|3 years ago

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

saturn_vk|3 years ago

I always assumed most people were against it afraid of generics because they only had experience with dynamic typed languages and heard someone complain about c++ templates that one time.