Hacker News new | ask | show | jobs
by minamea 4782 days ago
Any plans to add templates/a form of generic programming? Does the community care about that? How have people been working around that?

And congrats!

EDIT: Yes I know what the faq says [1]. I was wondering if someone working on that can shed light on how the development of a satisfactory proposal has been going. Also, what's exactly wrong with template instantion like in C++ from the perspective of Go devs?

[1] http://golang.org/doc/faq#generics

4 comments

It gets brought up continually on the mailing list.

The Go authors have repeatedly stated that they will not be implementing generics.

They don't seem to miss them, I don't miss them, and indeed most people who write much Go don't seem to miss them. The people who continually whine about generics seem to be the type who say, "Oh boy, I'd sure LOVE to write a bunch of kickass code in Go, but I just couldn't do ANYTHING without generics! Why don't you put them in, then I'll try the tutorial"

It's not something we feel the need to work around, we just sit down and write concise programs that do the job, in fewer lines than C++.

Edit: as others have mentioned, it seems the authors would be willing to put them in if they find a way to do it without crapping up the language. If they do, I'll try writing Go code with generics, but they have a bad taste for me after dealing with code containing GenericType.cpp, GenericType.hpp, AbstractType.cpp, AbstractType.hpp, AbstractGenericType.cpp, and AbstractGenericType.hpp all in one place.

>The Go authors have repeatedly stated that they will not be implementing generics.

No, they have repeatedly made ho-hum excuses about how they can't think of any proper way of implementing them, and that they will do them if they find a "clean way".

>They don't seem to miss them, I don't miss them, and indeed most people who write much Go don't seem to miss them. The people who continually whine about generics seem to be the type who say, "Oh boy, I'd sure LOVE to write a bunch of kickass code in Go, but I just couldn't do ANYTHING without generics!"

Well, isn't this dismissal of them a kind of "confirmation bias"?

Sure, they people who don't care about generics can churn out tons of code without them. Like people have been churning out tons of code in C or pre-generics Java anyway. And, sure, they might not even miss them.

Plus, some of the core Go members have been using languages without generic for decades and even created some. So, being used to working like that, it makes sense for them not to miss them.

That doesn't mean the people who DO want them are wrong in wanting them in -- or that because guys writing C for several decades doesn't find a need for generics in Go, means that there really isn't one.

> The Go authors have repeatedly stated that they will not be implementing generics.

That's not true. We just don't have a way of doing them that works well in the language. http://golang.org/doc/faq#generics

Yeah, I updated to reflect that. I was basing that statement on the replies I'd see on golang-nuts when the weekly generics thread came up.
What about macros then?
Russ Cox wrote down in 2009 what's the dilemma about generics: http://research.swtch.com/generic I think this is a very valuable article to understand why the Go team hesitates to implement generics in some suboptimal form.
It also highlights Go's weak spot ... it's too low level, while not being low level enough for manual memory management.

The JVM engineers have been free to do whatever they want in terms of optimizations at runtime, like code inlining or escape analysis. They may even add trace compilation or whatever they feel like it's necessary. The JVM's GC is precise and generational. The CMS garbage collection rarely blocks the whole process and you've got options for GCs that are totally non-blocking. In fact, the JVM's GC is so efficient that allocating and deallocating short-lived objects is almost as efficient as doing that on the stack.

Russ Cox speaks about the boxing that Java does, but Java makes boxing almost a non-issue. Personally I've learned to appreciate the JVM and what it can do in the context of servers receiving tens of thousands of requests per second.

And here in lies the problem - Go is too low level to achieve the same level of optimizations that Java is able of. But on the other hand it's totally dependent on a per-process GC and personally I don't think we'll ever see a precise, generational GC for it precisely because Go is too low level. The Rust engineers at the very least redefined the problem by making individual threads in a process have their own heap and their own garbage collector.

Go on the other hand feels like a hack on multiple accounts. And to me it's not a beautiful hack either, plus once a language achieves inertia, you cannot change its fundamentals without redefining the language to be something else entirely. If Go will indeed get popular, it will join a long line of languages that people hate, because they have to maintain code-bases written in it, with no easy way out.

Its authors say they don't want Go to be like C++. In many ways, it already is.

> The JVM engineers have been free to do whatever they want in terms of optimizations at runtime, like code inlining or escape analysis.

Both of these optimizations are present in this release of Go. Granted, there is no doubt more inlining opportunities exist, and better escape analysis is possible, but I think you'd want to chose different optimizations to highlight your point.

What specifically makes you think that there'll never be a precise, generational GC for Go? What about Go makes it too low-level for such an implementation. Unless I'm missing some key issue, I definitely think its possible...

> Go on the other hand feels like a hack on multiple accounts.

Again, what specifically about the language gives you this impression?

> If Go will indeed get popular, it will join a long line of languages that people hate, because they have to maintain code-bases written in it, with no easy way out.

Forget about evidence, you're making claims with absolutely no reasoning behind them. What is different about Go as opposed to say Python, or Erlang, or Node, or C++ in this situation? A code-base written in any of these languages needs maintenance... I don't see how Go is any different in this situation.

One of the 3 original designers of Go is Robert Griesemer, who worked on the Java HotSpot compiler, so I think it's safe to say that the Go team is quite aware of what the JVM can and cannot do.
So? Is this like an appeal to authority?
Wow, you even manage to put a negative spin on "the whole Go team has an impressive proven track record."
This is a bad attitude, imo. The Go standard library wouldn't be possible without generics [1]. It's just that the functionality exposed to the standard library isn't exposed otherwise.

If it's a feature needed for the standard library, then it's safe to say the devs will need it to. The devs aren't (shouldn't) be consistently writing programs that are more trivial than the standard library.

[1] http://golang.org/doc/effective_go.html#append

Not sure I agree with this logic.

I could easily write a custom Append func in my own application because I would know the Slice types I'm trying to operate on. Writing a language feature is different than writing an application, and it has nothing to do with how trivial something is.

Honestly, Append() as a concept is about as trivial as it gets..

> I could easily write a custom Append func in my own application because I would know the Slice types I'm trying to operate on.

More precisely, you would write one custom Append function per Slice type. AppendString, AppendInt, AppendWhatever.

Yeah, that was supposed to be singular: because I would know the Slice type I'm trying to operate on. Appreciate the clarification. I'll leave the typo there for context.
> Writing a language feature is different than writing an application

True enough. But libraries can easily tend toward either of those extremes. And any obstacles to library development will eventually become obstacles to application development.

You could also say that ANSI C has fenced off generics (array of T). Just as with Go's append and co, that's part of the language, not it's standard library.

http://golang.org/pkg/builtin/#pkg-overview

The thing is, you can do that with interface{} and you don't even need type switching. It's awkward, but considering that the rarity with which interface{} is necessary (at least in my experience), much less interface{} with type switching, I really don't see a big gain in generics. I agree with jff, it seems most people who complain about the lack of generics are those who haven't written much code without generics.
You lose a lot of type safety via interface{}. I'm currently writing a server that makes heavy use of []interface{} and I hate it. For many, most, maybe even all, code pathes in the app I could 100% static-type if Go allowed for something akin to Haskell's generies. Basically I bundle around a type `APIVal` (an alias for []interface{}), but everywhere I either take as an argument or return an APIVal, I can tell you exactly what type the underlying slice items "really" have, but I can't assert that at compile time, and it kinda bugs me.
>>The Go authors have repeatedly stated that they will not be implementing generics.

Not so http://golang.org/doc/faq#generics; "This remains an open issue." In the discussions I have read, if an implementation of generics is proposed that doesn't sacrifice performance and it truly generic, they are open to it.

What I have seen said is that implementing generics was not on the table for 1.1 or maybe even 1.x

>In the discussions I have read, if an implementation of generics is proposed that doesn't sacrifice performance and it truly generic, they are open to it.

Sounds like weasel speak for "take a hike and leave us alone".

Or maybe professor speak for freshmen Johnny who thinks he as an answer to the the Halting problem...

But in all seriousness they are interested in implementing generics, just not in a hurry to do it. It doesn't seem unreasonable to me to label it a low priority feature compared to other things they have added to Go. Java got along just fine before generics (which are arguably poor) and C still gets along fine without them. The Go authors are saying, lets add Generics when we are all happy about the solution and feel we are going to get it "right".

>>Any plans to add templates/a form of generic programming?

The Go team is open to generics as soon as there is a proposal they feel fits right without performance penalties. Read: http://golang.org/doc/faq#generics

>>Does the community care about that?

Yes, but most people in the Go community are OK waiting for the "right" solution.

>>How have people been working around that?

Generics are nice and convenient, but you can write anything/everything without generics. Just look at everything written in C. And C doesn't have interfaces like Go.

>The Go team is open to generics as soon as there is a proposal they feel fits right without performance penalties.

Because using interface{} for the same tasks fits so much better and has so much better performance right?

Tons of languages have implemented Generics -- it's not rocket science since at least 2 decades.

If the Go team is not interesting in working on the thing, then I doubt they would be really evaluating any "proposal".

I think Generics are just low priority for the Go team. Maybe we will see them in Go 2.0

>>tons of languages have implemented Generics -- it's not rocket science since at least 2 decades.

Sure, but that doesn't mean there are not concerns about the way it is often implemented: http://research.swtch.com/generic

I haven't looked at go beyond doing the tutorial, so what follows isn't a learned opinion. The interface system in Go covers most of what you would do with generics, if you use interfaces your code is generic. The only friction over a generics implementation is the way you wire up types to interfaces.
Someone correct me if I'm wrong, but I think that's true only if you use methods and not operators, because Go doesn't support operator overloading.
The other place you really feel the lack of generics is in the collections. They are built around the empty interface so that they can hold any type. Then you have to cast back to what you want. Exactly like Java pre generics. ugh.
That's correct, which means stonemetal's comment is true only for non-math code, but 99% of the code out there is not math.