既然型別引數在 上可用golang/go:master,我決定試一試。我似乎遇到了在Type Parameters Proposal 中找不到的限制。(或者我一定錯過了)。
我想撰寫一個函式,該函式回傳具有介面型別約束的泛型型別的值。如果傳遞的型別是帶有指標接收器的實作,我們如何實體化它?
type SetGetter[V any] interface {
Set(V)
Get() V
}
// SetGetterSlice turns a slice of type V into a slice of type T,
// with T.Set() called for each entry in values.
func SetGetterSlice[V any, T SetGetter[V]](values []V) []T {
out := make([]T, len(values))
for i, v := range values {
out[i].Set(v) // panic if T has pointer receiver!
}
return out
}
當SetGetterSlice()使用*Countas 型別呼叫上述函式時T,此代碼將在呼叫時發生恐慌Set(v)。( Go2go playground ) 毫不奇怪,代碼基本上創建了一個nil指標片段:
// Count implements SetGetter interface
type Count struct {
x int
}
func (c *Count) Set(x int) { c.x = x }
func (c *Count) Get() int { return c.x }
func main() {
ints := []int{1, 2, 3, 4, 5}
sgs := SetGetterSlice[int, *Count](ints)
for _, s := range sgs {
fmt.Println(s.Get())
}
}
同一問題的變體
這個想法行不通,我似乎找不到任何簡單的方法來實體化指向的值。
out[i] = new(T)將導致編譯失敗,因為它回傳*T型別檢查器想要查看的位置T。- 呼叫
*new(T), 編譯但將導致相同的運行時恐慌,因為在這種情況下new(T)回傳**Count,指向的指標Count仍然是nil。 - 將回傳型別更改為指向的指標切片
T將導致編譯失敗:
func SetGetterSlice[V any, T SetGetter[V]](values []V) []*T {
out := make([]*T, len(values))
for i, v := range values {
out[i] = new(T)
out[i].Set(v) // panic if T has pointer receiver
}
return out
}
func main() {
ints := []int{1, 2, 3, 4, 5}
SetGetterSlice[int, Count](ints)
// Count does not satisfy SetGetter[V]: wrong method signature
}
解決方法
到目前為止,我發現的唯一解決方案是要求將建構式傳遞給泛型函式。但這只是感覺不對,而且有點乏味。如果func F(T interface{})() []T語法完全有效,為什么需要這樣做?
func SetGetterSlice[V any, T SetGetter[V]](values []V, constructor func() T) []T {
out := make([]T, len(values))
for i, v := range values {
out[i] = constructor()
out[i].Set(v)
}
return out
}
// ...
func main() {
ints := []int{1, 2, 3, 4, 5}
SetGetterSlice[int, *Count](ints, func() *Count { return new(Count) })
}
概括
我的問題,按優先順序:
- 我是否忽略了一些明顯的東西?
- 這是 Go 中泛型的限制嗎?
- 這個限制是已知的還是我應該在 Go 專案中提出問題?
uj5u.com熱心網友回復:
提案中有一個例子涵蓋了這種情況,在指標方法示例段落的深處:
type Setter2[B any] interface {
Set(string)
*B // non-interface type constraint element
}
基本上,您必須再添加一個型別引數以SetGetter進行限制,T以便稍后您可以將其轉換為指標。
你的約束已經宣告了一個型別 param V,所以我們稍微修改上面的例子:
// V is your original type param
// T is the additional helper param
type SetGetter[V any, T any] interface {
Set(V)
Get() V
*T // "type *T" in the current Go2 playground
}
然后SetGetterSlice用型別引數定義函式T any,其目的只是實體化約束SetGetter。
然后,您將能夠將運算式轉換為&out[i]指標型別,并成功呼叫指標接收器上的方法:
// T is the type with methods with pointer receiver
// PT is the SetGetter constraint that also maps T to *T
func SetGetterSlice[V any, T any, PT SetGetter[V, T]](values []V) []T {
out := make([]T, len(values))
for i, v := range values {
// out[i] has type T
// &out[i] has type *T
// PT constraint includes *T
p := PT(&out[i]) // valid conversion!
p.Set(v) // calling with pointer receiver
}
return out
}
完整程式:
package main
import (
"fmt"
)
type SetGetter[V any, T any] interface {
Set(V)
Get() V
*T // "type *T" in Go2 playground
}
func SetGetterSlice[V any, T any, PT SetGetter[V, T]](values []V) []T {
out := make([]T, len(values))
for i, v := range values {
p := PT(&out[i])
p.Set(v)
}
return out
}
// Count implements SetGetter interface
type Count struct {
x int
}
func (c *Count) Set(x int) { c.x = x }
func (c *Count) Get() int { return c.x }
func main() {
ints := []int{1, 2, 3, 4, 5}
// instantiate with base type
sgs := SetGetterSlice[int, Count](ints)
for _, s := range sgs {
fmt.Println(s.Get()) // prints 1,2,3,4,5 each in a newline
}
}
正如提案還指出的那樣,這變得有點冗長,因為SetGetterSlice現在需要三個型別引數,
V any: 價值T any: 帶有指標接收器的型別PT SetGetter[V,T]: 實際約束
但是當你呼叫這個函式時,你可以省略第三個;由于型別推斷,型別引數V和T實體化約束所需的引數PT SetGetter[V,T]都是已知的:
SetGetterSlice[int, Count](ints)
Go2 游樂場:https ://go2goplay.golang.org/p/wLKNJIAAITT
uj5u.com熱心網友回復:
您也可以嘗試稍微不同地解決問題,以保持簡單。
package main
import (
"fmt"
)
func mapp[T any, V any](s []T, h func(T) V) []V {
z := make([]V, len(s))
for i, v := range s {
z[i] = h(v)
}
return z
}
func mappp[T any, V any](s []T, h func(T) V) []V {
z := make([]V, 0, len(s))
for _, v := range s {
z = append(z, h(v))
}
return z
}
// Count implements SetGetter interface
type Count struct {
x int
}
func (c *Count) Set(x int) { c.x = x }
func (c *Count) Get() int { return c.x }
func FromInt(x int) *Count {
var out Count
out.x = x
return &out
}
func main() {
ints := []int{1, 2, 3, 4, 5}
sgs := mapp(ints, FromInt)
fmt.Printf("%T\n",sgs)
for _, s := range sgs {
fmt.Println(s.Get())
}
fmt.Println()
sgs = mappp(ints, FromInt)
fmt.Printf("%T\n",sgs)
for _, s := range sgs {
fmt.Println(s.Get())
}
}
https://go2goplay.golang.org/p/vzViKwiJJkZ
它就像你的,func SetGetterSlice[V any, T SetGetter[V]](values []V, constructor func() T) []T但沒有復雜的冗長。它也讓我零痛苦解決。
uj5u.com熱心網友回復:
編輯:請參閱blackgreen 的答案,我后來在掃描他們鏈接的相同檔案時也自己找到了答案。我打算編輯此答案以基于此進行更新,但現在我不必這樣做了。:-)
可能有更好的方法 - 這個方法看起來有點笨拙 - 但我能夠解決這個問題reflect:
if reflect.TypeOf(out[0]).Kind() == reflect.Ptr {
x := reflect.ValueOf(out).Index(i)
x.Set(reflect.New(reflect.TypeOf(out[0]).Elem()))
}
我剛剛將上述四行添加到您的示例中。臨時變數是一些除錯遺留下來的,顯然可以洗掉。 游樂場鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/323591.html
上一篇:與布爾型LKP_value&ANDLKP_Array的INDEX/MATCH
下一篇:Kotlin錯誤Smartcastto'X'是不可能的,因為'state'是一個在嘗試觀察狀態時具有打開或自定義getter的屬性
