I am looking at the pointer method example from generics doc. There it states:
This approach works as expected, but it is awkward to have to repeat Settable in the type arguments. Fortunately, constraint type inference makes it less awkward.
But if I convert that example into methods on a struct, the type interference does not make it less awkward. Demo
package main
import "strconv"
type Settable int
func (p *Settable) Set(s string) {
i, _ := strconv.Atoi(s)
*p = Settable(i)
}
type Setter[B any] interface {
Set(string)
*B // non-interface type constraint element
}
type F[T any, PT Setter[T]] struct{}
func (_ F[T, PT]) FromStrings(s []string) []T {
result := make([]T, len(s))
for i, v := range s {
p := PT(&result[i])
p.Set(v)
}
return result
}
func main() {
_ = F[Settable, *Settable]{}.FromStrings([]string{"1", "2"})
_ = F[Settable]{}.FromStrings([]string{"1", "2"})
}
The last line fails compiling. Are there plans to improve this?
I am looking at the pointer method example from generics doc. There it states:
But if I convert that example into methods on a struct, the type interference does not make it less awkward. Demo
The last line fails compiling. Are there plans to improve this?