代數的 data 型 in Go
直積型 (product type) は type embed 埋め込む
code:go
type A struct{}
type B struct{}
type AAndB struct {
A
B
}
明示
code:go
type A struct {
Aa int64
}
type B struct {
Bb string
}
type AAndB struct {
Aa int64
Bb string
}
直和型 (sum type) は type set of a union code:go
type A struct{}
type B struct{}
type AOrB interface {
A | B
}
switch v := any(x).(type) {
case A:
// treat v as a A
case B:
// treat v as a B
}
}
兩方の型に共通するものを型 cast せずに使へんかね?
runtime なら
code:go
switch v := x.(type) {
case A:
// treat v as a A
case B:
// treat v as a B
default:
panic(fmt.Errorf("%v is a %T", x, v))
}