News for hackers. Stuff that matters.
Re:Why Generics? by haolez on Wednesday July 31, 2019 @03:12PM (#20577243)
Personally, I prefer the current interfaces-based solution to generics. It's a little verbose, but keeps the language simple.

However, I think that the people we should be listening most are the ones developing huge projects in Go, like Kubernetes. Would having generics with this new contracts thing make it easier to develop and maintain e.g. Kubernetes? I'm truly curious.

Re:Why Generics? by btilly on Wednesday July 31, 2019 @03:46PM (#20577651)
Which interfaces-based solution?

The one used by sort is a hack that only works for some of the things that you would want to do.

The one where you use introspection performs badly and isn't typesafe.

The tree implementation in the article cannot be done in a safe and performant way in Go today.

Re:Why Generics? by haolez on Wednesday July 31, 2019 @05:41PM (#20578862)
The one mentioned in the article.

It's not a hack: it's a runtime equivalent that requires that your type implements some interface.

Re:Why Generics? by btilly on Wednesday July 31, 2019 @08:07PM (#20580001)
It is a hack. The article lists a whole series of things that are standard in other languages that you can't do in go. Here are some examples:

  Find smallest/largest element in slice
  Find average/standard deviation of slice
  Compute union/intersection of maps
  Find shortest path in node/edge graph
  Apply transformation function to slice/map, returning new slice/map
And here are data structures that other languages have but go does not:

  Sets
  Self-balancing trees, with efficient insertion and traversal in sorted order
  Multimaps, with multiple instances of a key
  Concurrent hash maps, supporting parallel insertions and lookups with no single lock
If it were not a hack, then go would not have these limitations.
Re:Why Generics? by majewsky on Thursday August 01, 2019 @09:02AM (#20583331)
> Multimaps, with multiple instances of a key

Isn't this just map[Key][]Value? The standard library uses these in several places, e.g. url.Values and http.Header, with assorted utility functions.

Re:Why Generics? by btilly on Thursday August 01, 2019 @02:13PM (#20586623)
You should send that feedback to the author of the article. I literally just cut and pasted those lists.
Re:Why Generics? by silasdavis on Wednesday July 31, 2019 @03:39PM (#20577571)
Probably yes: https://medium.com/@arschles/go-experience-report-generics-i...

Many smaller projects would benefit too. I would like to build a typesafe tree for an efficient sorted map, and to be able mergsort over multiple trees of different types. It would allow some extremely useful channel combinators for doing rather common things like safely shutting down a service with some background processing. Often these things leak and a often I find myself spawning more go routines just to map between types or to stop generic interface types infecting the rest of the API.

Re:Why Generics? by jerf on Wednesday July 31, 2019 @03:51PM (#20577718)
I have been vigorously pro-generics since the beginning, and this is the reason why: I want generic data structures. Arrays&slices and maps are great, and they really are the 90/10 solution a lot of the time, which is precisely why putting direct support into your syntax for the two of them is so very, very popular, but that other 10 comes up.

Plus, there are some generic data structures that will really work well in Go, like, for instance, an immutable tree. Granted, it'll still take some care to use properly in Go as it does not have "const" or anything like it, but it can still be done. The problem I have is not with accidental mutation, but that I just don't want to sit there and implement the immutable tree code. (Trees are great, but they're really tedious to write in the best of times, and nightmares to debug in the worst.)

I'm not terribly interested in trying to jam functional programming into Go; I may make light use of map/filter/reduce but even if this was fully implemented it would still be a fairly unpleasant experience (function that return "a value and an error" aren't much fun to map and can't hardly "chain" at all). But I've missed being able to just grab a particular data structure a few times.

Re:Why Generics? by silasdavis on Wednesday July 31, 2019 @04:01PM (#20577854)
I agree with the 90/10. An example of something I use in its non type safe generic form that would benefit: https://github.com/eapache/channels
Re:Why Generics? by noncoml on Wednesday July 31, 2019 @03:37PM (#20577552)
Generics and Interfaces solve different problems
Re:Why Generics? by ci5er on Wednesday July 31, 2019 @07:14PM (#20579589)
Yes - but don't they get contangled?

How would you re-orthoganize the problem statement? (Statements, actually)

Re:Why Generics? by dotaheor on Wednesday July 31, 2019 @09:52PM (#20580534)
Yes, I think. Now, the kubernetes project uses too many code generators, which makes it is to extend.
Re:Why Generics? by saturn_vk on Thursday August 01, 2019 @05:21AM (#20582308)
I take it you've never actually read the K8s source code.
[dead]
Re:Why Generics? by dang on Wednesday July 31, 2019 @10:35PM (#20580702)
Oh dear. Personal attacks are not ok on HN, regardless of whom you're attacking. Please stick to the site guidelines, no matter how passionately you feel about the Liskov Substitution Principle.

https://news.ycombinator.com/newsguidelines.html

You obviously have a good point to make, but it needs to be done without firebombs.

Re:Why Generics? by rsc on Wednesday July 31, 2019 @04:47PM (#20578367)
> Rot in hell forever, Josh. And take your Effective Java with you to throw on the pyre.

Well that seems uncalled for.

Re:Why Generics? by hinkley on Wednesday July 31, 2019 @06:54PM (#20579450)
It’s fairly difficult to come to terms with an “everything was going pretty well until...” moment and doubly so when it can be personified.

Once we broke LSP, a cascade of similar ideas started showing up with regularity, especially in J2EE. I used to be able to go on at some length but now it’s more “I hope I never meet this guy because it’ll be awkward as hell.”

Re:Why Generics? by ben509 on Wednesday July 31, 2019 @05:56PM (#20578967)
> The only truly unforgiveable one is UnsupportedOperationException. The guy who wrote the collections API for Java didn't know the first thing about the Liskov Substitution Principle, and gave the world an implementation that violates it...

The implementation perfectly follows it because the LSP simply requires identical behavior, and the contract clearly states it may[1] throw an exception. All the implementations "may" throw an exception. Sorry, but they correctly lawyered the LSP.

> I came up with around 20% more than the selected design.

But `add` can throw 5 different exceptions depending on restrictions your implementation wants to place on a given collection, does it allow for all that? In terms of making an interface that was small and reusable by many projects, runtime exceptions are a pretty good compromise for a standard library.

[1]: https://docs.oracle.com/javase/7/docs/api/java/util/Collecti...

Re:Why Generics? by hinkley on Wednesday July 31, 2019 @06:48PM (#20579405)
No they most definitely are not. One implementation receives a message. The other categorically rejects it. I cannot substitute them.
Re:Why Generics? by ben509 on Thursday August 01, 2019 @10:32PM (#20590306)
Yes, those are the same behavior. Both of them "may" accept it and "may" reject it, and "may" includes both "always" and "never".

It is correct to the letter of LSP, but not the spirit of it, and this was a conscious decision; contrary to your original claim, they understood the principle perfectly well.

Re:Why Generics? by extempore on Wednesday July 31, 2019 @04:11PM (#20577965)
Of course they do. Not sure why it’s something to brag about or not to brag about though, it’s an implementation detail. Scala for instance uses custom classes for each size set up to a certain threshold.
Re:Why Generics? by YawningAngel on Wednesday July 31, 2019 @03:35PM (#20577521)
Kubernetes is a transpiled Java project, so yeah, Java-like features would definitely make it easier :P

I think idiomatic golang works pretty well without generics in most cases, the big problem for me is that functional programming is essentially impossible without them.

Re:Why Generics? by yureka on Wednesday July 31, 2019 @03:41PM (#20577590)
Can you provide some resources on what makes you think Kubernetes is a transpiled Java project?

That's definitely not the case from what I know/understand...

Re:Why Generics? by tuvan on Wednesday July 31, 2019 @03:48PM (#20577679)
There is a talk about it you can find here: https://fosdem.org/2019/schedule/event/kubernetesclusterfuck...
Re:Why Generics? by snazz on Wednesday July 31, 2019 @04:08PM (#20577932)
It was originally written in Java and lots of its Go code is a strange mix of the two (Java-like idioms in Go). This might be where the term "Gova" came from.
Re:Why Generics? by pjmlp on Wednesday July 31, 2019 @04:48PM (#20578381)
The original team used Java, and the actual team rewrote it in Go.
Re:Why Generics? by jjtheblunt on Wednesday July 31, 2019 @04:07PM (#20577915)
https://en.wikipedia.org/wiki/Kubernetes says original Kubernetes was all C++.
Re:Why Generics? by eshyong on Wednesday July 31, 2019 @04:33PM (#20578208)
If you read the article more closely, it says that Borg was written in C++. If you watch the video linked by @tuvan below, you'll hear the speaker (who is a contributor) mention that the original authors of Kubernetes wrote the first version in Java, which was rewritten in Go.
Re:Why Generics? by archy_ on Wednesday July 31, 2019 @03:33PM (#20577484)
Agreed on keeping the status quo, adding generics makes it really easy to write bad code and increases mental overhead. Simplicity is what we should be going for, not having generics should've been kept as a selling point.
Re:Why Generics? by silasdavis on Wednesday July 31, 2019 @03:44PM (#20577623)
This may be practical from the point of view of 'top level applications' in Go. But there is a lot of murk in pure Go implementations of databases, and datastructure heavy libraries that could be avoided and lead to better testing and less duplication. If you find it simpler to avoid generics that ought to be possible under this design.