News for hackers. Stuff that matters.
Re:Idiomatic Generics in Go by tomp on Tuesday September 30, 2014 @11:20AM (#8388664)
> It doesn't matter how clever those features are, overall simplicity and consistency matters more.

You mean, except the lack of simplicity when you have generic arrays and maps, and the lack of consistency when no other type can be generic? (Personally, I don't think generics are complicated, but people say that Go designers think so.)

Re:Idiomatic Generics in Go by kyrra on Tuesday September 30, 2014 @11:29AM (#8388727)
Generics add addition type checking at the the cost of complexity in other areas: syntax, compilation, runtime[0]. Heavily debated there, but what they are trying to get at is that generics have a cost associated with them, adding them is not free. The Go team is trying to search to see if there is some better way to solve this problem right now.

As well, from what I've heard of Java's generics implementation, it add a huge amount of complexity to javac and the JRE. The Go team is small and probably didn't have the resources to do it out the gate.

[0] http://research.swtch.com/generic

Re:Idiomatic Generics in Go by bad_user on Tuesday September 30, 2014 @02:00PM (#8389844)
Generics added complexity to Java because of:

1. if you have sub-typing in the language, then you'll naturally have to think of co/contra-variance, which leads us to ...

2. Java had backwards compatibility concerns, for example they were unwilling to deprecate the standard collections, so they chose to implement use-site variance with wildcards

There are several problems there - first of all you cannot declare variance rules on the classes themselves, even though there are legitimate use-cases for that, like immutable data-structures have no problem in being covariant. Hence generics in Java are much less useful than they would be with declaration-site variance. Then subtyping + variance, especially with wildcards which are basically existential types, is undecidable, so by definition people have a hard time in producing a reliable compiler.

Here's a fun example that could crash a compiler:

      class C<P> implements List<List<? super C<C<P>>>> {}
And doesn't that suck?

But seriously, Java's designers could have chosen a better, saner path. Declaration-site variance is so much better. And we aren't talking about really expressive type-systems, with higher-kinded types and type-2 polymorphism, for starters the basics would do just fine.

I mean, Go's standard data-structures are generified. Why not provide the same capability to its users? This situation is exactly like with Java's arrays.

Re:Idiomatic Generics in Go by tomp on Tuesday September 30, 2014 @11:46AM (#8388847)
I don't think you understood my point. To repeat: there is no cost in adding generics to Go, because they are already there!

They are just non-generic (pun intended) - you cannot add it to every type, only to "blessed" types - arrays/slices, channels and maps.

Re:Idiomatic Generics in Go by burntsushi on Tuesday September 30, 2014 @07:13PM (#8391962)
> To repeat: there is no cost in adding generics to Go, because they are already there!

Could you please stop spreading this nonsense?[1] Generics aren't free. You will always pay for it in terms of some combination of implementation complexity, runtime performance and compiler performance.

[1] - https://news.ycombinator.com/item?id=8316995

Re:Idiomatic Generics in Go by tomp on Wednesday October 01, 2014 @05:29AM (#8393498)
I'm sorry. In all honesty, I have no idea how generics are currently implemented in Go. If you know, I'd kindly ask you to explain why you think the current implementation could not be extended to user-defined types.

> Generics aren't free. You will always pay for it in terms of some combination of implementation complexity, runtime performance and compiler performance.

I don't argue about implementation complexity and compiler performance, I'm just saying that I assume that most that complexity is already present in the Go compiler.

However, I think that saying that generics have runtime performance cost is disingenuous. It's like saying that functions aren't free. Now, obviously it's possible to program without function by using loops, gotos or inlining code. However, if you want to make your program more abstract, you'll end up implementing functions, along with calling conventions, register saving, varargs, ... It's easier if the compiler does that for you (also, sometimes the compiler will compile functions into loops, gotos or inlined code, reducing the performance penalty).

It's the same with generics. If you have an algorithm that you want to use with different data types, you can either (1) implement it multiple times, or (2) manually box and unbox all values. That's exactly what the compiler can do for you if you write the code using generics. The fact that most compilers use just one of these strategies is not a deficiency of generics, but the deficiency of compilers. Both Scala and .Net use both specialization and boxing. Therefore, in all honesty, I really see no additional runtime cost to generics that wouldn't be present in equivalent non-generic code.

Re:Idiomatic Generics in Go by burntsushi on Wednesday October 01, 2014 @07:08AM (#8393726)
> It's like saying that functions aren't free.

They aren't free...

I literally don't know what to say here. The whole point is to analyze the trade offs of generics rather than start out with a judgment that they are always worth it. In order to have generics supported in a language, you must pay for it some way or another. If you just box all of the values like (I think) you're suggesting and provide compiler support for it, then programs that use generics are going to pay a cost in runtime performance compared to programs that don't use them. In other implementations of generics, it's possible to remove that runtime cost (or almost remove), but usually at the cost of something else like implementation complexity or compiler speed. Programs that use generics with that implementation choice won't pay a runtime cost compared to programs that don't use them. This obviously has a significant impact on the decision procedure of whether to use generics or not at any given point in your program.

And yes, absolutely, saying generics has a cost is exactly like saying functions have a cost. The only difference between them is that there is clearly still a substantive amount of disagreement over whether full blown generics is always worth its weight in value (compared to the cost of not having them).

Currently, Go does not have anything resembling the term "generics." (Except for a few built in types and functions defined in the language specification.)

Re:Idiomatic Generics in Go by GhotiFish on Tuesday September 30, 2014 @05:28PM (#8391382)
boy I hear you there. That drives me completely insane about Go. They choose to have generics in these specific instances because if they didn't the language would be cumbersome. It's not enough.
Re:Idiomatic Generics in Go by NateDad on Tuesday September 30, 2014 @11:29AM (#8388726)
It's still a vastly simpler language than pretty much any other widely used language out there. Consistency is superseded by pragmatism in the language design.
Re:Idiomatic Generics in Go by latch on Tuesday September 30, 2014 @11:38AM (#8388784)
C ?
Re:Idiomatic Generics in Go by norswap on Tuesday September 30, 2014 @11:50AM (#8388872)
Go actually manages to be less powerful than C. You could do this generic stuff with macros in C.

Honestly, Go is probably one of the only language that is clearly not expressive enough.

Re:Idiomatic Generics in Go by zuzun on Tuesday September 30, 2014 @01:15PM (#8389446)
I'm sorry but this is utter nonsense and shows the quality of most "Go lacks generics" discussions. Go manages to do so many things so much better than C [0], the boldness of your statement leaves me speechless.

[0] Just to name a few: Go has packages instead of header files, one simple build command instead of Makefiles, embedded structs, interfaces, allows you to declare functions everywhere, multiple return values, panics, a huge (!) platform independent standard library, native platform independent concurrency, saner string handling, lists, dictionaries, iota, cross-platform and cross-architecture code via file names instead of macros + autotools, ...

I really can't understand how people dismiss all of this and claim a programming language is useless because it lacks a single feature.

Re:Idiomatic Generics in Go by norswap on Tuesday September 30, 2014 @06:23PM (#8391726)
You misunderstand me.

I am not saying C does things better than Go. I am saying it is less expressive.

In the sense that there are a lot of things you simply cannot implement in C, whereas you can implement them -- in an ugly and unsafe fashion, it is true -- in C.

Generics is not the only example. You can easily get exceptions in C with setjmp and longjmp for instance. You can even get garbage collection in C (and not only talking about the conservative Boehm stuff, you can use fat pointers -- yes it's unwieldy and ugly, but it works).

As for all the niceties, yes there are there, but you can implement most of them in C also (and, in fact, people have).

You'll notice I did not even say C was better than Go.

My actual thoughts on the matter is that they're not made for the same use cases -- albeit one of my problem with Go was always figuring its use case. Apparently I'm not the only one confused, because the language creators expected to get C users, they got Python user instead. As you said yourself, it has more niceties, so maybe it is kind of a middleground when more performance or concurrency is needed, but C is deemed to tricky or unsafe. As I have learned to navigate my ways around C's pitfalls (and boy, there are some pitfalls!), I don't think I'll ever be tempted to use Go (at least, if the language doesn't change significantly).

I'm also not some kind of C fanatic, I use Java, Ruby or Python if it gets the job done; and while I don't really use them for actual work, I have a soft spot for some functional languages.

Re:Idiomatic Generics in Go by bitg on Wednesday October 01, 2014 @12:46PM (#8395855)
You could compare any language to machine language and you can say that language is less powerful. Go is not even close to the power of C and assembly. I'm not gonna even try to argue.
Re:Idiomatic Generics in Go by pessimizer on Tuesday September 30, 2014 @01:23PM (#8389512)
I think that when you call something utter nonsense, and name a lot of reasons why you think it's utter nonsense, you should be obligated to explain at least one. It's not clear why the features you mention are better than their C equivalents.
Re:Idiomatic Generics in Go by NateDad on Tuesday September 30, 2014 @02:44PM (#8390241)
Uh, which of those aren't just blatantly obvious? (Totally serious question)
Re:Idiomatic Generics in Go by pessimizer on Wednesday October 01, 2014 @11:00AM (#8394907)
None of them are obvious to me at that level of detail. (Totally serious answer)
Re:Idiomatic Generics in Go by masklinn on Tuesday September 30, 2014 @12:38PM (#8389213)
Well it's pretty close to Java pre-1.5, which I guess was fine back in the 90s.
Re:Idiomatic Generics in Go by norswap on Tuesday September 30, 2014 @06:26PM (#8391747)
Precisely, more than 10 years have passed. And Java evolved for a reason, I think. And releasing Java without generics may not have been a smart thing even back then.
Re:Idiomatic Generics in Go by NateDad on Tuesday September 30, 2014 @02:05PM (#8389877)
Except go has value types and closures and first class functions and implicit interfaces and multiple return values....
Re:Idiomatic Generics in Go by pjmlp on Tuesday September 30, 2014 @06:12PM (#8391664)
Yeah it wins over Java 1.5, but it looses against so many 80's and 90's languages, which offer all of that and, wait for it...., generics.
Re:Idiomatic Generics in Go by NateDad on Tuesday September 30, 2014 @02:34PM (#8390154)
Oh yeah, and generic lists and maps And built-in testing infrastructure, performance testing, code distribution, linting, code formatting, parsing and AST libraries...
Re:Idiomatic Generics in Go by papaf on Tuesday September 30, 2014 @04:54PM (#8391150)
And a built in dependency manager, build tool, documentation generator...
Re:Idiomatic Generics in Go by dkarapetyan on Tuesday September 30, 2014 @06:10PM (#8391653)
But generics crosses the line because compile-time code generation is so much clearer and simpler and doesn't add any overhead to compilation times because code substitution is not compilation.
Re:Idiomatic Generics in Go by pjmlp on Tuesday September 30, 2014 @06:13PM (#8391670)
pre-Java 1.5 already had all of that.
Re:Idiomatic Generics in Go by burntsushi on Tuesday September 30, 2014 @07:16PM (#8391976)
Where was pre-Java 1.5's generic HashMap? Or closures?
Re:Idiomatic Generics in Go by pjmlp on Wednesday October 01, 2014 @03:23AM (#8393208)
> Where was pre-Java 1.5's generic HashMap?

In java.util.Hashtable as implementation of the java.util.Dictionary interface, since version 1.1 back in 1997.

Implemented using s/interface{}/Object/g with ability to use any type as key, as long as, hashCode() and equals () are overriden.

Also allows for fine tuning of the capacity and load factor.

> Or closures?

In anonymous inner classes. A pain to use when compared with real closures, but doable nonetheless.

Re:Idiomatic Generics in Go by burntsushi on Wednesday October 01, 2014 @06:59AM (#8393707)
That is not a generic HashMap. See masklinn's response.

The fact is, the comparison between Go and "pre generics Java" is a bad one. There are many substantial differences, and blessed parameterized types in Go is absolutely one of them.

Re:Idiomatic Generics in Go by masklinn on Wednesday October 01, 2014 @05:42AM (#8393535)
In the discussion's context, "generic" is about parameterized types (type-safe collections), not about working on any object.
Re:Idiomatic Generics in Go by pjmlp on Wednesday October 01, 2014 @07:01AM (#8393710)
Which Go lacks and the OP was stating it has.
Re:Idiomatic Generics in Go by masklinn on Wednesday October 01, 2014 @09:09AM (#8394071)
* Go has a generic hashmap, the builtin map type is one of the magical special-status generic collections (with arrays, slices and channels).

* pre-1.5 Java did not have one, its only parametric type was the low-level array

Re:Idiomatic Generics in Go by pjmlp on Wednesday October 01, 2014 @10:25AM (#8394531)
Ah, ok. That is correct.