Re:Generics enabled by default in Go tip
by dmitriid on Saturday August 21, 2021 @02:19AM
(#28254698)
> Generics would let you enforce the type of `foo` at compile time, but it wouldn't save you many lines of code.
You literally showed that "for each type of paged object, you write <multiple lines of entirely unnecessary code>".
Where with generics you just have a single generic function.
> We hand-wave over that in the above example with
The problem is: there's no hand-waving in reality.
Re:Generics enabled by default in Go tip
by dmitriid on Saturday August 21, 2021 @08:07AM
(#28256310)
I'm traveling, so can't properly expand on what I said, but without generics the choice was:
- repeat unnecessary bolierplate code for every single return type
- skip types completely and use a function that accepts interface{} as a parameter and assigns whatever to that variable
Neither are really acceptable in my opinion. And both will be greatly simplified and improved by generics.
Let me conclude with a recommendation for gRPC and gRPC-gateway as a bridge to clients that don't want to speak gRPC. Then you can just return a "stream Foo", and the hard work is done for you. You call stream.Next() and get a Foo object ;)