reflect: implement Type.AssignableTo

This commit is contained in:
Ayke van Laethem
2019-08-08 13:05:17 +02:00
committed by Ron Evans
parent b8cd8b6f25
commit 614433cb75
2 changed files with 14 additions and 6 deletions
+12
View File
@@ -216,6 +216,18 @@ func (t Type) Size() uintptr {
}
}
// AssignableTo returns whether a value of type u can be assigned to a variable
// of type t.
func (t Type) AssignableTo(u Type) bool {
if t == u {
return true
}
if t.Kind() == Interface {
panic("reflect: unimplemented: assigning to interface of different type")
}
return false
}
type StructField struct {
Name string
Type Type
+2 -6
View File
@@ -388,12 +388,8 @@ func (v Value) Set(x Value) {
if !v.indirect {
panic("reflect: value is not addressable")
}
if v.Type() != x.Type() {
if v.Kind() == Interface {
panic("reflect: unimplemented: assigning to interface of different type")
} else {
panic("reflect: cannot assign")
}
if !v.Type().AssignableTo(x.Type()) {
panic("reflect: cannot set")
}
size := v.Type().Size()
xptr := x.value