The authors of the current generics proposal mention that they looked into
doing generics only on package level:(
https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#why-not-put-type-parameters-on-packages
)
I understand the reasons what it wasn't pursued further.
Did the authors investigate the possibility of declaring generic types on a
package level instead of per type/function? Ie.
generic T {}
func Max(a, b T) T {}
func Min(a, b T) T {}
I wrote down an idea on this, that is admittingly a bit wild (or naive) or
maybe impossible from a parsing perspective. But I'd like to validate the
main idea behind it.
My whole attempt was based on the observation that most packages that will
include generic functionality, will only really need one or maybe a couple
of generic type constraints.
For example, if you look at a hypothetical statistics package, there will
be many, many functions that compute stuff on slices of T, where T must be
a number. So there will be many functions like:
type Number interface { // using currently proposed generics
type int, float64, // etc
}
func Average(type T Number)(values T...) T {} // using currently proposed
generics
func Median(type T Number)(values T...) T {}
func Binonimal(type T Number)(values T...) T {}
// etc etc etc
My point is that the "(type T Number)" part in all the declarations /
definitions become really redundant from a human perspective, just because
a package will naturally (should?) only use 1 or at most a couple of
generic types. That would make it, at least theoretically, possible to
write something like
type Number interface {
int, float64, // etc
}
generic T Number
func Average(values T...) T {}
func Median((values T...) T {}
// etc
Coincidentally, the builtin language sort of uses this idea in its
documentation.
Another (theoretic) possibility would be something like this:
generic T Number ( // analog to multiple const or var declarations
func Average(values T...) T {}
func Median((values T...) T {}
)
My question is: did the authors investigate something like this, or is it
plainly impossible or undesired?
For those interested, my proposal can be found here:
https://github.com/golang/go/issues/39716. I use the key word "meta" there
instead of "generic".
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/245ce96d-52e4-4763-9296-58b8fdf26b1bn%40googlegroups.com.