I regard current Team's proposal a way better than the first iteration. But -
while I see a need for generic way to write common code, I also share
concerns about readability and future abuse of the materialized Go generics
being similar to "the other languages". I consciously did not patricipate in
the
[Generics, please go
away!](https://groups.google.com/g/golang-nuts/c/LEEuJPOg0oo)
bikeshed, but I would like to share an idea that may possibly reconcile both
camps.
"Signature switch" - readable generic code preserving the Go way.
Generic function declaration is
https://golang.org/ref/spec#Function_declarations
with at least one type in the Signature given as the ascii character "+"
followed
by single capital ascii letter, further reffered to as "TypeLetter"; and with a
single,
non empty, signature switch present in the function body:
func f (x +T) (r +R) { // generic function
switch func.(type) { // Signature Switch here. Resolves at COMPILE
TIME.
case func(x int) (r int): // call-site signature to match
r = x/2 // instantiated code, T and R are now
ints
}
return r
}
More to read at: https://play.golang.org/p/Q1ry4KPoOOJ
Hope this helps,
--
Wojciech S. Czarnecki
<< ^oo^ >> OHIR-RIPE
--
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/20201231193635.0d27177a%40xmint.