reflect: implement New function

This is very important for some use cases, for example for Vecty.
This commit is contained in:
Ayke van Laethem
2021-04-11 13:50:44 +02:00
committed by Ron Evans
parent 57271d7eaa
commit e587b1d1b4
8 changed files with 87 additions and 25 deletions
+15
View File
@@ -124,6 +124,21 @@ func main() {
showValue(reflect.ValueOf(v), "")
}
// Test reflect.New().
newInt8 := reflect.New(reflect.TypeOf(int8(0)))
newInt8.Elem().SetInt(5)
newInt16 := reflect.New(reflect.TypeOf(int16(0)))
newInt16.Elem().SetInt(-800)
newInt32 := reflect.New(reflect.TypeOf(int32(0)))
newInt32.Elem().SetInt(1e8)
newInt64 := reflect.New(reflect.TypeOf(int64(0)))
newInt64.Elem().SetInt(-1e12)
newComplex128 := reflect.New(reflect.TypeOf(0 + 0i))
newComplex128.Elem().SetComplex(-8 - 20e5i)
for _, val := range []reflect.Value{newInt8, newInt16, newInt32, newInt64, newComplex128} {
showValue(val, "")
}
// test sizes
println("\nsizes:")
for _, tc := range []struct {
+25
View File
@@ -321,6 +321,31 @@ reflect type: ptr
embedded: false
reflect type: int addrable=true
int: 42
reflect type: ptr
pointer: true int8
nil: false
reflect type: int8 settable=true addrable=true
int: 5
reflect type: ptr
pointer: true int16
nil: false
reflect type: int16 settable=true addrable=true
int: -800
reflect type: ptr
pointer: true int32
nil: false
reflect type: int32 settable=true addrable=true
int: 100000000
reflect type: ptr
pointer: true int64
nil: false
reflect type: int64 settable=true addrable=true
int: -1000000000000
reflect type: ptr
pointer: true complex128
nil: false
reflect type: complex128 settable=true addrable=true
complex: (-8.000000e+000-2.000000e+006i)
sizes:
int8 1 8