You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: pointer to concrete type doesn't satisfy generic type method set #48512
package main
import"fmt"typeFooerinterface{ Foo() }
typeFooGetter[FFooer] interface{ GetFooer() F }
typeAstruct{}
func (A) GetFooer() B { returnB{} }
typeBstruct{}
func (B) Foo() {}
funcfun[FFooer, FGFooGetter[F]](a*FG) F {
returna.GetFooer()
}
funcmain() {
fmt.Printf("%T", fun[B, A](&A{}))
}
works on playground, but doesn't on latest tip. I suspect this is correct behavior since simplified version doesn't compile either on go2go playground. But then, why does go2go permits this code?
go2go was only created to experiment with the type parameters proposal when no implementation existed, the fact that it does or does not permit some piece of code doesn't mean much in itself.
x.go:18:11: a.GetFooer undefined (type *FG has no field or method GetFooer)
The type *FG (pointer to type parameter) doesn't have a GetFooer method, but the type FG does. This code compiles:
package main
import"fmt"typeFooerinterface{ Foo() }
typeFooGetter[FFooer] interface{ GetFooer() F }
typeAstruct{}
func (A) GetFooer() B { returnB{} }
typeBstruct{}
func (B) Foo() {}
funcfun[FFooer, FGFooGetter[F]](aFG) F { // <<< FG not *FGreturna.GetFooer()
}
funcmain() {
fmt.Printf("%T", fun[B, A](A{})) // <<< A{} not &A{}
}
There's no automatic indirection for pointers to type parameters - maybe there should be (we're still fine-tuning those rules).
I don't know why your original version works on the go2go playground - possibly because the translated code does the automatic indirection (because it's been translated into regular Go types). But it doesn't really matter - we're not going to dig into that - the playground was for experimentation only is not kept up tp date. I recommend you use the latest compiler as the ground truth at this point. Thanks.
Activity
ALTree commentedon Sep 21, 2021
go2go was only created to experiment with the type parameters proposal when no implementation existed, the fact that it does or does not permit some piece of code doesn't mean much in itself.
cc @griesemer
griesemer commentedon Sep 21, 2021
The error message is pretty clear:
The type
*FG(pointer to type parameter) doesn't have aGetFooermethod, but the typeFGdoes. This code compiles:There's no automatic indirection for pointers to type parameters - maybe there should be (we're still fine-tuning those rules).
I don't know why your original version works on the
go2goplayground - possibly because the translated code does the automatic indirection (because it's been translated into regular Go types). But it doesn't really matter - we're not going to dig into that - the playground was for experimentation only is not kept up tp date. I recommend you use the latest compiler as the ground truth at this point. Thanks.Closing as there is nothing to do here.
DmitriyMV commentedon Sep 21, 2021
@griesemer sorry to bother you a little more, but what is the correct way to require pointers on callee side?
griesemer commentedon Sep 21, 2021
Pass a pointer type argument in
main:fmt.Printf("%T", fun[B, *A](&A{}))Also, see the section on constraint type inference. Thanks.