Ian Lance Taylor schrieb am Mittwoch, 19. August 2020 um 00:39:16 UTC+2:

> Yes, but here you are assigning the value struct{}{} to the type S. 
> That's not how type arguments work: type arguments are not assigned to 
> type parameters. Instead, the body of a generic function or type is 
> instantiated with the type argument. In a generic function, rather 
> than assigning a value struct{}{} to type S, we are replacing 
> instances of S in F with struct{}. But struct{} has no methods. So 
> can you call method M on an argument whose type is the type parameter? 
> Why or why not? 
>

Thanks for the explanation. 

Given this setup:

    type S struct{}
    func (s S) M() {}

    func F1[T S](s T) { s.M() }
    func F2[T struct{}](s T) {}

The instantiations would have to be:

    F1[S] :: func(S)
    F1[struct{}] :: func(S)

    F2[struct{}] :: func(struct{})
    F2[S] :: func(struct{})

Non-interface/non-"anyof" constraints would always be instantiated as 
themselves.
I can see that this might be surprising. Given that it doesn't add any 
value over normal function parameters it is probably not worth it.

-- 
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/9087e39e-65f0-49bc-8952-d50d6353d256n%40googlegroups.com.

Reply via email to