blob: c51dd6f826735feded1559a3851954dcf513dd09 [file] [log] [blame] [view]
Andrew Bonventrefde42b62018-08-28 09:56:58 -06001# Go 2 Generics Feedback
2
3This page is meant to collect and organize feedback about the Go 2 [contracts (generics) draft design](https://go.googlesource.com/proposal/+/master/design/go2draft-generics-overview.md).
4
5Please post feedback on your blog, Medium, GitHub Gists, mailing lists, Google Docs, etc. And then please link it here.
6
7As the amount of feedback grows, please feel free to organize this page by specific kind of feedback.
8
Liamc0662122018-08-28 22:33:12 -07009 - Liam Breck, “[Please Don't Mangle the Function Signature](https://gist.github.com/networkimprov/7c1f311f26852bc912765e4110af062b)”, August 2018
10
DeedleFake1de36842018-08-29 02:05:40 -040011- DeedleFake, "[Feedback for Go 2 Design Drafts](https://deedlefake.com/2018/08/feedback-for-go-2-design-drafts/)", August 2018
12
Roberto94c8e9a2018-08-29 13:59:07 +020013- Roberto (empijei) Clapis, "[Hard to read syntax](https://gist.github.com/empijei/a9665ac5e3059671be229acee8826798)", August 2018
Robertof76be042018-08-29 11:20:54 +020014
Liam6d4eebc2018-08-28 22:29:09 -070015 - _Your Name_, “[_Title_](#URL)”, _month year_
16
Andrew Bonventrefde42b62018-08-28 09:56:58 -060017 - etc.
Dag Sverre Seljebotn3841f012018-08-28 21:07:53 +020018
19
20## Quick comments
21
Maxwell Corbin9144ad32018-08-28 12:58:11 -070022 - Dag Sverre Seljebotn: C++ has a huge problem with people abusing metaprogramming ("generics") to do compile-time metaprogramming. I really wished Go had gone down the path of Julia, which offers hygienic macros. Even if it is kept strictly at a compile-time barrier and no run-time code generation, this would at least avoid all the bad tendencies we see in the C++ world that comes from their templating system. Things you can do with generics you can usually pull off with macros too (e.g., `SortSliceOfInts = MakeSliceSorterFunctionMacro!(int)` could generate a new function to sort a slice of integers). Link: https://docs.julialang.org/en/v0.6.1/manual/metaprogramming/
23
24 - Maxwell Corbin: The issues raised in the Discussion and Open Questions section all could be avoided by defining generics at the package rather than the function or type level. The reason for this is simple: types can reference themselves, but packages can't import themselves, and while there are many ways to algorithmically generate more type signatures, you cannot do the same with import statements. A quick example of such syntax might be:
Takuma Ishikawa4e952c22018-08-29 13:37:30 +090025
26 ```go
27 \\ list
28 package list[T]
29
30 type T interface{}
31
32 type List struct {
33 Val T
34 Next *List
35 }
36
37 // main
38 package main
39
40 import (
41 il "list"[int]
42 sl "list"[string]
43 )
44
45 var iList = il.List{3}
46 var sList = sl.List{"hello"}
47
48 // etc...
49 ```
50
51 The syntax in the example is probably needlessly verbose, but the point is that none of the unfortunate code examples from the blog post are even legal constructions. Package level generics avoids the most abusive problems of meta-programming while retaining the bulk of its usefulness.
Maxwell Corbin9144ad32018-08-28 12:58:11 -070052
Russell Johnston31d12712018-08-28 20:51:02 -070053 - Andrew Gwozdziewycz: The use of the word `contract` gives me pause due to it overloading "contract" as in [Design by Contract](https://en.wikipedia.org/wiki/Design_by_contract). While the generics use case has some similarities with the "contracts" in DbC if you squint a bit, the concepts are quite different. Since "contracts" are an established concept in Computer Science, I think it would be far less confusing to use a different name like `behavior` or `trait`. The design document also suggests reasons why using `interface` is not ideal, though, Go's contract mechanism seems too obvious an extension of interfaces to disregard so quickly... If it can be done `interface setter(x T) { x.Set(string) error }` and `interface addable(x T, y U) { x + y }` seem quite natural to read and understand.
54
Hajime Hoshif82716f2018-08-29 12:58:25 +090055 - Russell Johnston: Agreed that it would be great to merge contracts and interfaces. Another way around the operator-naming problem might be to provide some standard interfaces for the operators, with bodies inexpressible in normal Go code. For example, a standard `Multipliable` interface would allow the `*` and `*=` operators, while a standard `Comparable` interface would allow `==`, `!=`, `<`, `<=`, `>=`, and `>`. To express operators with multiple types, these interfaces would presumably need type parameters themselves, for example: `type Multipliable(s Self /* this exists implicitly on all interfaces */, t Other) interface { /* provided by the language */ }`. Then user-written interfaces/contracts could use these standard identifier-based names, neatly sidestepping the issues mentioned in the design document around syntax and types.
Roberto6059b202018-08-29 14:57:35 +020056 - Roberto (empijei) Clapis: I agree on this and on the fact that it should be clearer where to use interfaces and where to use contracts. Unifying the two would be great, as they try to address overlapping issues.
Hajime Hoshif82716f2018-08-29 12:58:25 +090057
Hajime Hoshi52978612018-08-29 13:01:29 +090058- Hajime Hoshi: I feel like the supposed proposal is too huge to the problems we want to solve listed at https://go.googlesource.com/proposal/+/master/design/go2draft-generics-overview.md . I'm worried this feature would be abused and degrade readability of code. Sorry if I am missing, but the proposal doesn't say anything about `go generate`. Wouldn't `go generate` be enough to the problems?
Stephen Rowlesc831c582018-08-29 11:45:23 +010059
60- Stephen Rowles: I find the method syntax hard to parse, as a human reading it, it might be clearer to use a different type of enclosing brackets for the type section, e.g.
61
Stephen Rowlesbb469892018-08-29 11:45:49 +010062 ```
63 func Sum<type T Addable>(x []T) T {
64 var total T
65 for _, v := range x {
66 total += v
67 }
68 return total
69 }
Robertoc46f1692018-08-29 13:55:11 +020070 ```
Roberto6059b202018-08-29 14:57:35 +020071 - Roberto Clapis: Please read [this section](https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md#why-not-use-like-c_and-java)
Roberto5e630f62018-08-29 14:59:34 +020072 - Seems like a bit of a cop-out tbh. It says "in general" which means there must already be exceptions. Go has a nice clear syntax making code simple to read and easy for teams to collaborate. I think it would be worth making the parser more complicated for the sake of making the code readability better. For large scale and long running project readability of the code, and hence maintainability, is king
73 - What about [this](https://gist.github.com/empijei/a9665ac5e3059671be229acee8826798)