reflect: implement Type.AssignableTo
Этот коммит содержится в:
родитель
b8cd8b6f25
коммит
614433cb75
2 изменённых файлов: 14 добавлений и 6 удалений
|
@ -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 {
|
type StructField struct {
|
||||||
Name string
|
Name string
|
||||||
Type Type
|
Type Type
|
||||||
|
|
|
@ -388,12 +388,8 @@ func (v Value) Set(x Value) {
|
||||||
if !v.indirect {
|
if !v.indirect {
|
||||||
panic("reflect: value is not addressable")
|
panic("reflect: value is not addressable")
|
||||||
}
|
}
|
||||||
if v.Type() != x.Type() {
|
if !v.Type().AssignableTo(x.Type()) {
|
||||||
if v.Kind() == Interface {
|
panic("reflect: cannot set")
|
||||||
panic("reflect: unimplemented: assigning to interface of different type")
|
|
||||||
} else {
|
|
||||||
panic("reflect: cannot assign")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
size := v.Type().Size()
|
size := v.Type().Size()
|
||||||
xptr := x.value
|
xptr := x.value
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче