型パラメータのソースコード読みメモ
Termの列を2つ取ってintersectionをとる関数。列の長さをN, MとしてO(NM)になっていることがコメントに書かれている。今の状態がわかりやすい。
lazy union type set computationというものが出てくる(コメントに)
遅延して計算させるということだと思うけど具体的にどういうことかが気になる
ok == true
であってもどうせnil
チェックが必要で、ok == false
の場合もnil
が帰るので、型アサーションが成功していることのチェックとnilでないことのチェックを1つにまとめることができる。なるほど
griesemer氏はしばしばuser-defined typeの意味でdefined typeと書く
勉強会で見たコミット
CLはこっちに移った
プルリクチャンス
Implementation restriction:
A union expression with more than one term cannot contain interface types
with non-empty method sets.
の記述は不正確である
理由
The method set of an interface type is the intersection of the method sets of each type in the interface's type set (which is usually just the set of declared methods in the interface).
とある。そのため、次のMyIntIF
を考えると、float64 | MyIntIF
というunions
がillegalになるが、これはlegalになるべきである。
type MyInt int
func(x MyInt) Print() {}
type MyIntIF interface {
MyInt
}
type SomeIF interface {
// illegal according to the spec because MyIntIF is an interface type with non-empty method set.
// but I believe it should be legal.
MyIntIF | float64
}
より詳しく述べる。
The method set of an interface type is the intersection of the method sets of each type in the interface's type set
より、MyIntIFのメソッドセットはMyIntIFの型セットに属する型のメソッドセットの共通部分である。また、
-
MyIntIF
の型セットは{ MyInt }
である -
MyInt
のメソッドセットは{ Print() }
である
ので、method_set(MyIntIF) = { Print() }
となる。よって、1より大きい数のtermからなるunionsはMyIntIF
を含むことができないことになるが、https://github.com/golang/go/issues/45346#issuecomment-862505803 の趣旨から考えればこれは許されるはずである。
typeparamsって入ってるissue
CLプレビューツール by syumaiさん
go install golang.org/dl/gotip@latest
gotip download dev.typeparams
gotip run -gcflags="-G=3" main.go
typeparamsってついているissues
According to this line, following definition of IFUnion should not be allowed. But I think this should be allowed:
type MyInt int
func (MyInt) Print() {} // method set of MyInt is { Print() }
type MyIntIF interface {
MyInt
}
type IFUnion interface {
int | MyIntIF
}
According to L851, the method set of MyIntIF is { Print() }, which is not empty.
So IFUnion contains a union with interface type with non-empty method sets, making IFUnion not allowed.
I wonder if this is what this line means.