Go1.18의 Generics로 할 수 없는 일.

14824 단어 Gotech

개시하다


며칠 전에 고1.18 나왔습니다.
보기노트 를 발행 하다 고1.18시 limitations라고 쓰여 있기 때문에 코드를 써서 확인해야 합니다.
모두 여섯 개가 있다.
!
일본어 부분은 간단한 필기이니 참고하세요.

첫번째


The Go compiler cannot handle type declarations inside generic functions or methods. We hope to provide support for this feature in Go 1.19.
generic의 함수와 방법에서 형식을 발표할 수 없습니다.
package main

import "fmt"

func main() {
	fmt.Println("Hello, 世界")

}

func GenericsF[T any]() {
	// [NG] type declarations inside generic functions are not currently supported
	type s struct{}

}

func F() {
	type s struct{}
}
관련 issue: https://github.com/golang/go/issues/47631

두번째


The Go compiler does not accept arguments of type parameter type with the predeclared functions real, imag, and complex. We hope to remove this restriction in Go 1.19.
generic형의 매개 변수를 리얼 등 미리 발표된 함수에 전달할 수 없습니다.
package main

import "fmt"

type MyFloat float64

func main() {
	fmt.Println("Hello, 世界")

}

func GenericsF[T ~float64](a T) {
	// [NG] a (variable of type T constrained by ~float64) not supported as argument to complex for go1.18 (see issue #50937)
	_ = complex(a, a)
}
관련 issue: https://github.com/golang/go/issues/45049

셋째


The Go compiler only supports calling a method m on a value x of type parameter type P if m is explicitly declared by P's constraint interface. Similarly, method values x.m and method expressions P.m also are only supported if m is explicitly declared by P, even though m might be in the method set of P by virtue of the fact that all types in P implement m. We hope to remove this restriction in Go 1.19.
명확하게 발표하는 방법만 쓸 수 있다.
package main

import "fmt"

func main() {
	fmt.Println("Hello, 世界")
}

type S1 struct{}

func (s S1) m() {}

type S2 struct{}

func (s S2) m() {}

type Constraint interface {
	S1 | S2
	m2()
}

func GenericsF[P Constraint](x P) {
	// [NG] x.m undefined (type P has no field or method m)
	x.m()
	
	// [OK]
	x.m2()
}

네 번째


The Go compiler does not support accessing a struct field x.f where x is of type parameter type even if all types in the type parameter's type set have a field f. We may remove this restriction in Go 1.19.
type parameter에게 맡길 수 있는 모든 유형이 공통된 영역이 있어도 그 영역을 방문할 수 없습니다.
package main

import "fmt"

type S1 struct {
	f int
}

type S2 struct {
	f int
}

type Constraint interface {
	S1 | S2
}

func main() {
	fmt.Println("Hello, 世界")

}

func GenericsF[T Constraint](x T) {
	// NG: x.f undefined (type T has no field or method f)
	fmt.Println(x.f)
}

관련 issue: https://github.com/golang/go/issues/48522

다섯 번째


Embedding a type parameter, or a pointer to a type parameter, as an unnamed field in a struct type is not permitted. Similarly, embedding a type parameter in an interface type is not permitted. Whether these will ever be permitted is unclear at present.
type parameter 및 포인터는 포함할 수 없습니다.
package main

import "fmt"

type Embed[T any] struct {
	field T // OK
	T       // NG: embedded field type cannot be a (pointer to a) type parameter

}

type EmbedPointer[T any] struct {
	field *T // OK
	*T       // NG: embedded field type cannot be a (pointer to a) type parameter
}

type I[T any] interface {
	T | int // NG: cannot embed a type parameter
}

func main() {
	fmt.Println("Hello, 世界")
}

여섯 번째


A union element with more than one term may not contain an interface type with a non-empty method set. Whether this will ever be permitted is unclear at present.
비공식 방법 집합이 있는interface형은 유니언 요소에 사용할 수 없습니다.
package main

import "fmt"

// interface type with a non-empty method set
type I1 interface {
	Func()
}

type I2 interface{}

type I3 interface {
	I1 | I2 // NG: cannot use main.I1 in union (main.I1 contains methods)
}

type I4 interface {
	int32 | int64
}

type I5 interface {
	float32 | float64
}

type I6 interface {
	I4 | I5 // OK
}

func main() {
	fmt.Println("Hello, 世界")
}


Go1.19 이정표는 여기.에서 볼 수 있다.
"이렇게 안 할 거야?"발생하면 찾을 수 있을지도 몰라요.

좋은 웹페이지 즐겨찾기