FWIW I think that one possible approach to allowing methods (and
potentially function values) with type parameters might be to allow
instantiating them only with a limited set of "shapes" of data (for
example, only pointer-like types). Then I think there's the possibility
that the method or function could be compiled once for each allowed shape
but still allow instantiation with an arbitrary number of types, without
the necessity to statically enumerate all the possible types for that
function.

We need to instantiate p1.S.Identity[int] somewhere, but how?


If one did something like the above, I guess you'd look up the
implementation by shape (8 bytes, non pointer) from the type info in the
interface value, then call the compiled function with the appropriate meta
information based on that. Details left as an exercise for the reader :)

I don't see that reflection per se would need to be involved, any more than
reflection is involved when doing a dynamic type conversion.

  cheers,
    rog.

On Wed, 13 Jan 2021 at 20:38, Marcus Manning <icons...@gmail.com> wrote:

>
> 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 golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/420e8a74-d9f8-4df9-9d7b-efffb642fbe8n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/420e8a74-d9f8-4df9-9d7b-efffb642fbe8n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAJhgaci2EutMD9dxpeiW%3DrKapyHUad7mSL7VxMjLVFt7NQXamA%40mail.gmail.com.

Reply via email to