Regarding the the section of No parameterized methods in go generics draft:
package p1 // S is a type with a parameterized method Identity. type S
struct{} // Identity is a simple identity method that works for any type.
func (S) Identity[T any](v T) T { return v } package p2 // HasIdentity is
an interface that matches any type with a // parameterized Identity method.
type HasIdentity interface { Identity[T any](T) T } package p3 import "p2"
// CheckIdentity checks the Identity method if it exists. // Note that
although this function calls a parameterized method, // this function is
not itself parameterized. func CheckIdentity(v interface{}) { if vi, ok :=
v.(p2.HasIdentity); ok { if got := vi.Identity[int](0); got != 0 {
panic(got) } } } package p4 import ( "p1" "p3" ) // CheckSIdentity passes
an S value to CheckIdentity. func CheckSIdentity() { p3.CheckIden
But package p3 does not know anything about the type p1.S. There may be no
other call to p1.S.Identity elsewhere in the program. We need to
instantiate p1.S.Identity[int] somewhere, but how?
The natural way would be to fetch instance sets (sets containing method
implementations for any specified interface, here HasIdentity) as a matter
of reflection.
However, solving instance resolution over reflection alludes to the
following questions:
- Does reflection support generic instantiation?
- Are generic types/functions are part of the runtime type information
selectable for runtime reflection?
- Are type to interface conformance relationships are part of the
runtime type information?
If all these questions can be answered by yes, then the case highlighted
above is solvable.
Alternatively, modulating the "CheckIdentity" function by the compiler to:
func CheckIdentity(v interface{},
hiddenHasIdentityInstanceContainingPointerToIdentityMethod) { if vi, ok :=
v.(p2.HasIdentity); ok { if got := vi.Identity[int](0); got != 0 {
panic(got) } } }
serves providing an instance automatically by calling "CheckIdentity"
function. This hidden parameter needs to be up-propagated to all callers
passing p1.S down the caller-callee hierarchy.
--
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/420e8a74-d9f8-4df9-9d7b-efffb642fbe8n%40googlegroups.com.