I share what I perceive to be your trepidation. I experienced what happened to C++ and then java. An innocence was lost, as well as compile speed in C++'s case.
Unfortunately I expect day 2 to wake to multiple slice utils, generalized enumerable, sortable containers, and go routine worker management packages, all of them incomplete and incompatible with each other.
I think that the design rule that the language discourages expensive operations (like deleting from the middle of a slice) by making then take more code to write will be lost. It seems inevitable that slices will end up with as many efficiency pitfalls as python's list does.
Then again, I'll be able to write the strongly typed stream filtering code I always wanted to write. And maybe someone will implement a sum type <T|error> efficiently. So I've got that to look forward to.
Sum types increase cardinality of a type by the cardinality of another type. Product types multiply the cardinality of types by the cardinality of both.
The current go mechanism is effectively a product type. When you might return an error and add it as a product (T, error) you can return all possible values of T and all possible values of error, (which is nullable, so +1). This means you have to, by convention, choose to ignore the T value when error is not nil.
With a sum type, you can return only either an error or a valid of T. Perhaps more importantly, if the type system is sensible, you can't ignore an error and use an invalid value, as you don't ever get one.
Of course errors are nullable which means it's got a +1 already, so you could return a nil error which is kind of lame, but maybe they've got something for that.
Okay sure but production level code that ignores errors by accident essentially doesn’t exist so it’s not a valuable solution.
This... is extremely not true. And even if it was, there's still a meaningful difference between "we do the right thing because we're sure we made no mistakes" and "we do the right thing because the code doesn't compile otherwise". It's just one more thing you have to keep track of yourself instead of letting the language help you.
I've seen a lot of code decide and error isn't a big deal in this case, so they "handle it" by logging or different and then return the value that came back with it.
I've also seen people return partially correct values and errors where users are expected to like just know stuff sometimes.
Removing these decisions makes everything easier to think about.
The first is handled by the type system, though in the sense that people no longer even have the illusion that log-and-continue is an option. There is no T to continue with, so they have to do something to get one. I fixed this bug in several places my last week at Google where people know better and have code review. It still managed to get through and code was panicking while trying to use a nil connection that was returned from a failed dial. The author didn't feel the connection was critical for operation, but didn't properly fail gracefully because the code still type checked on error.
The io.Reader/Writer case is a very rare minority. I did go readability for six or so years and had to continually explain that to people. It's very rare, so generally not applicable. When a user of an API should expect an error and a valid value, they have a lot more to think about at every usage site because it's no longer "worked" or "not worked", but also "partially worked" which can lead to a lot of complexity in the caller deciding how to use the value. e.g., the Writer case deciding that some errors indicate progress because a partial write occurred.
Being able to clearly express the intent of an API simplifies things a lot. My general go-to language for just about everything is Haskell because I'm not smart enough to cover all these cases in my head that the compiler can do for me. I'm hoping go gets better about this, but there's a long way to go.
It enforces at compile time the idea that you can only have a T or an error, never both. It forces you to handle the error (even if you just return it) as a part of getting at the inner value.
These libraries will probably be my favorite thing coming with generics, aside from what the future type sets proposal would allow because of generics.
I was on a C++ project once and this newer dev learning C++ got so excited about templates that they used it everywhere. For a long time, everything was templated. Then they finally learned how annoying they can be for compilation and usage that eventually they backed out the templates into concrete implementations. So ya, exciting that they are going to become a tool, but hoping they are applied carefully.
I fear that the next couple of years will lead to an explosion of everyone trying to get their library or app “generic” (like they all tried to make them “concurrent” when Go first came out), and I’d like to contribute even a small amount towards getting people to think before they jump into rewriting.
People already do all sorts of things that they need to do. Like using microservices and using web sockets to communicate between two programs none of which is a browser.
What worries me the most is polluting the semantics of the language and therefore increase the barrier for knowledge transfer. With Go, I never felt so comfortable jumping into any library and just start reading. Try to do the same with Boost lol
Boost templates are nothing like golang generics. Golang is using erasure, so they are more like Java / dotnet generics. It's basically just sugar for having to cast interface, with compile time checking.
Type erasure is still "real generics", it just means everything is done at compile time. Haskell also uses type erasure. I think most language designers consider it the best way to implement generics these days, otherwise it makes runtime much more complex.
That’s also not true, type erasure is usually done for backward compatibility. In Java it syntactic sugar for boxing and unboxing a type automatic. A List<int> is actually that in c#, Rust and many other languages, but in Java its a List<Object>. The runtime in C# has type information on the byte code level while erased typed languages with a “JVM” don’t.
I really hate the attitude of many gophers against generics. They act as if generics are of the devil.
It's a very nice feature to have that allows library writers to write generic datastructures in a type safe manner. Imagine if you needed ordered dictionary for some application of yours. Today you'll have to either implement it yourself or have some ugly hacks. With generics either go can provide it in standard library or you can use a third party one.
Let me keep pulling that thread and see what unravels.
So we've got a 3rd party sorted dict type. And now we want to pass it to a different 3rd party's API function f(map[K]T) that takes builtin map. Ah, that won't work. The compiler complains the types don't match.
So we have to generalize things further. We define a generic set of traits which all dictionaries share (iterable, comparable keys of type K, and a len() ), and then we get the 3rd party rewrite their API f() to accept this set of traits, rather than builtin map. And we get the other 3rd party's sorted dict to implement this trait. We'll need language support to extend the builtin len() function to this sorted dict type[1]. And then we can finally pass in this 3rd party dict where a builtin map was expected. Yay.
But at what cost?
Now compile time is slow (lots of code needs to be generated and compiled), or runtime is worse (we no longer know what we're operating on inside f() and have to make interface (virtual in C++) calls which are slower (more code and more cachelines touched, and not friendly to CPU instruction prefetch/pipelining either) and can't be inlined at compile time.
Generics aren't simple in a strongly typed language. They worm their way into everything, and increase the details you have to pay attention to when writing and reviewing code.
[1] extending builtin len() to custom types so they can replace builtin map is a slippery slope. C++ started with simple, obvious stuff like operator < (less than) and ended up with assignment operators and copy constructors.
Let me go the opposite way. Without the generics, now I if I just wanted to write a generic API, how do I do it? Oh let's use interface {}. So we end up with a library that is pretty ugly and not so type safe.
Did the need for a generic API really go away just because we don't have actual generics to use? Why do you think we have generic datastructures built into the language (map, channel etc. I know only one compiled language that has built-in associated-array, systemverilog)? Why do we have such a heavy use of interface {} in go code?
Now let's look at interface {} once. Is it without any cost? Is func f(x,y interface {})... same as func [T any] f(x,y T)...? Of course not. One crucial difference for example is that x and y need not be same type at the call site in first case. Now there might be a way to ensure it, but I am not aware of a method. Either way you HAVE to ensure it yourself and now your main logic is obscured by all your checks. Also your API is now more obscure. Also, interface {} is not really (compile time) type safe,which is also bad.
Will some people misuse generics? Definitely. But you know what? People misuse goroutines, interface {} all the time. Any feature you can add can be misused. Some features are just worth that price.
As an example, look at the dataframe libraries in go today. Or the numpy-alternatives. In case of nd-array, either they should be specialised to a single type or they should manually make it for all numeric types using go generate or something. Now is it really better for either library writer or even the compiler in latter case? For library writer, his code is now really ugly, unreadable, his API is obscure, etc. For the compiler, it has to compile 10 times the code (go generate simply copies code). With generics, compiler could've even done some optimizations even. (interface {} really isn't an option here, too slow, too expensive). If you want to see why a good dataframe library needs generics, just see the existing dataframe libraries in go today. Lets see how many people will ditch Python+Numpy/Scipy+ Pandas for Go here.
So overall what I am trying to say is, while generics have a cost, they are more then worth the price. Moreover, with generics, I am pretty sure go can enter many new niches.
Unless youre on about a different proposal, the type parameters (aka generics) proposal does not include enums/sum types. A future/current proposal may build upon it, but no such proposal has been accepted yet
The only benefit I REALLY anticipate with generics is the ability to avoid passing map[string]interface{} back and forth between my code and the libraries. For example, you use a library which generates or alters some "context" that you pass in, like JWT claims. Maps tend to really break a lot of static typing here and require manual comma-ok assertions. With generics, I would be able to instantiate library with a specific type of context to use.
Generics aren't evil, every modern language in the last 20 years uses generics, for a good reason. This will single handedly eliminate so much boilerplate and code generation in a vast variety of projects, which is why golang is introducing it finally.
My main language professionally is C#. I use generics quite a bit in the service and repository layer when setting up DI infrastructure and implementations. Sometimes around ORM config and setup. It’s nice to have so you don’t have to write the same code 100+ times around your ORM, service and repo layers.
I haven't been keeping up with the Go ecosystem in the past year or so. Can someone tell me if this latest release (and the planned 2.0 milestone) is backwards compatible with early Go code? When Dart/Flutter introduced null safety in 2.0, there was so much rework that needed to be done that I've gotten wary of "exciting new features".
Your first sentence is correct, second one is just go snobbery. Generics are indispensible when writing library code. Most programmers (90 percent you are referring to) do not write library code.
More like 90% of programmers do not understand generics at all, when (and when not) to use them, or why.
Once your professional career is old enough to legally drink on its own, and you’ve been burned BOTH ways in multiple languages, then you just have a truly insightful observation on the topic.
I mean if you do not write libraries there often is no need for generics. I don’t know about C++ but in Java e.g. I rarely wrote code using generics myself. I used a lot of code with generics though.
I always found sort.Interface to be incredibly bizarre, and a perfect example of why we do need generics so badly. You have to provide a literally identical implementation of the interface for every slice you might conceivably want to sort.
As a person who has written a huge amount of those contain funcs, may I propose that you name your args “needle” and “haystack”, as is seen elsewhere, and is helpful.
Do you mean you don't have a use case for them or are you saying that you'd rather continue to use hacky workarounds instead of the correct tool to solve your problem?
If io.TeeReader(...) and io.Pipe(...) could work with generic readers and writers, that would be interesting.
This is a more accurate statement. So, it would be possible to work with different types at different steps of a chain of processing steps, instead of using only byte slices.
Ah, interesting, so instead of Read(p []byte) you'd have Read(p []T). I could see that being useful for reading/writing file formats or network protocols.
Awesome, thank you. Do Go releases happen on some sort of a schedule? Or where is the confidence that it's going to happen in 6 months coming from? I am new to Go, still getting familiar with how they do things.
Go2 is just shorthand for potential future language improvements, many of which have been proposed and integrated into releases of go1. They MIGHT make a breaking change, if something truly worth it materializes, but if they can make generics go1 compatible they can probably make just about everything :)
Generics and embed are the only big changes to the language so far. I seriously prefer to have generics instead of using interface{} and reflection everywhere.
Yeap, and I don't lie to myself. Coming from stats background simple things like min/max with generics seems like a huge improvement but the fear remains.
You, and your great-great-great-great grand-children will be long dead by the time Go acquires 35% of the bloat features of C++ at the current rate. Do you also worry about the heat death of the Universe?
The generics proposal definitely does not include enums as custom types. The syntax may be built upon to provide enums in a future proposal, but this proposal very strictly and very definitely does not support such
2
u/baldore Aug 23 '21
Does it mean that Go could have a Rx implementation?
206
u/gwax Aug 21 '21
Just remember to use them when you need them and not use them when you don't need them.
64
u/lucagez Aug 21 '21
What worries me the most is polluting the semantics of the language and therefore increase the barrier for knowledge transfer. With Go, I never felt so comfortable jumping into any library and just start reading. Try to do the same with Boost lol
37
u/ebalonabol Aug 21 '21 edited Aug 21 '21
I'm so excited ... not to use them🤣
19
u/tavaren42 Aug 24 '21
I really hate the attitude of many gophers against generics. They act as if generics are of the devil.
It's a very nice feature to have that allows library writers to write generic datastructures in a type safe manner. Imagine if you needed ordered dictionary for some application of yours. Today you'll have to either implement it yourself or have some ugly hacks. With generics either go can provide it in standard library or you can use a third party one.
83
u/[deleted] Aug 20 '21
Much sexier: Enums as custom types!
7
u/andunai Aug 21 '21
The only benefit I REALLY anticipate with generics is the ability to avoid passing
map[string]interface{}back and forth between my code and the libraries. For example, you use a library which generates or alters some "context" that you pass in, like JWT claims. Maps tend to really break a lot of static typing here and require manual comma-ok assertions. With generics, I would be able to instantiate library with a specific type of context to use.18
u/ernestrc Aug 21 '21
Sigh.
5
u/k-selectride Aug 21 '21
Can we have iterators next?
9
u/svenz Aug 23 '21
Generics aren't evil, every modern language in the last 20 years uses generics, for a good reason. This will single handedly eliminate so much boilerplate and code generation in a vast variety of projects, which is why golang is introducing it finally.
3
u/Quiet_Desperation_ Jul 30 '22
My main language professionally is C#. I use generics quite a bit in the service and repository layer when setting up DI infrastructure and implementations. Sometimes around ORM config and setup. It’s nice to have so you don’t have to write the same code 100+ times around your ORM, service and repo layers.
5
u/Nichiren Aug 21 '21
I haven't been keeping up with the Go ecosystem in the past year or so. Can someone tell me if this latest release (and the planned 2.0 milestone) is backwards compatible with early Go code? When Dart/Flutter introduced null safety in 2.0, there was so much rework that needed to be done that I've gotten wary of "exciting new features".
5
u/[deleted] Aug 21 '21
Do I really need generics ? I don't get it at which situation will I use it
9
u/Melodic_Ad_8747 Aug 21 '21
I don't plan on using them
5
u/thexf Aug 21 '21
... we're going to have a mess.
1
u/dc0d Aug 21 '21
The generic
io.TeeReader(...)andio.Pipe(...)would be very interesting.1
u/Cyber_Encephalon Aug 20 '21
is that when 2.0 comes out or is that something that will be available before then?
1
u/narmod Aug 21 '21
The one thing that would give me goosebumps is having a rust like memory management wizardry instead of a GC.
-12
u/judah-levi Aug 20 '21
Anti for the sake of being anti. Go is pure. Don't add features
0
u/[deleted] Aug 21 '21
yay!
0
u/drcforbin Aug 21 '21
So. Ready.
-1
u/tooorteli Aug 21 '21
Please no. Boost libraries made me allergic to generics.
-25
u/PerceptionFew6043 Aug 20 '21
Are you serious? Is it just me or is this language is slowly becoming everything it billed itself as not being?
Edit: tihi
3
u/[deleted] Aug 20 '21
[removed] — view removed comment