mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-06 12:03:41 +00:00
reflect: add support for non-named basic types
This commit is contained in:
Vendored
+61
-1
@@ -1,6 +1,9 @@
|
||||
package main
|
||||
|
||||
import "reflect"
|
||||
import (
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type myint int
|
||||
|
||||
@@ -9,4 +12,61 @@ func main() {
|
||||
println(reflect.TypeOf(int(3)) == reflect.TypeOf(int(5)))
|
||||
println(reflect.TypeOf(int(3)) == reflect.TypeOf(uint(5)))
|
||||
println(reflect.TypeOf(myint(3)) == reflect.TypeOf(int(5)))
|
||||
|
||||
println("\nvalues of interfaces")
|
||||
for _, v := range []interface{}{
|
||||
true,
|
||||
false,
|
||||
int(2000),
|
||||
int(-2000),
|
||||
uint(2000),
|
||||
int8(-3),
|
||||
int8(3),
|
||||
uint8(200),
|
||||
int16(-300),
|
||||
int16(300),
|
||||
uint16(50000),
|
||||
int32(7 << 20),
|
||||
int32(-7 << 20),
|
||||
uint32(7 << 20),
|
||||
int64(9 << 40),
|
||||
int64(-9 << 40),
|
||||
uint64(9 << 40),
|
||||
uintptr(12345),
|
||||
float32(3.14),
|
||||
float64(3.14),
|
||||
complex64(1.2 + 0.3i),
|
||||
complex128(1.3 + 0.4i),
|
||||
"foo",
|
||||
unsafe.Pointer(new(int)),
|
||||
} {
|
||||
showValue(v)
|
||||
}
|
||||
}
|
||||
|
||||
func showValue(v interface{}) {
|
||||
rv := reflect.ValueOf(v)
|
||||
rt := rv.Type()
|
||||
if reflect.TypeOf(v) != rt {
|
||||
panic("direct TypeOf() is different from ValueOf().Type()")
|
||||
}
|
||||
println("reflect type:", rt.Kind().String())
|
||||
switch rt.Kind() {
|
||||
case reflect.Bool:
|
||||
println(" bool:", rv.Bool())
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
println(" int:", rv.Int())
|
||||
case reflect.Uint, reflect.Uintptr, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
println(" uint:", rv.Uint())
|
||||
case reflect.Float32, reflect.Float64:
|
||||
println(" float:", rv.Float())
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
println(" complex:", rv.Complex())
|
||||
case reflect.String:
|
||||
println(" string:", rv.String())
|
||||
case reflect.UnsafePointer:
|
||||
println(" pointer:", rv.Pointer() != 0)
|
||||
default:
|
||||
println(" unknown type kind!")
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+50
@@ -2,3 +2,53 @@ matching types
|
||||
true
|
||||
false
|
||||
false
|
||||
|
||||
values of interfaces
|
||||
reflect type: bool
|
||||
bool: true
|
||||
reflect type: bool
|
||||
bool: false
|
||||
reflect type: int
|
||||
int: 2000
|
||||
reflect type: int
|
||||
int: -2000
|
||||
reflect type: uint
|
||||
uint: 2000
|
||||
reflect type: int8
|
||||
int: -3
|
||||
reflect type: int8
|
||||
int: 3
|
||||
reflect type: uint8
|
||||
uint: 200
|
||||
reflect type: int16
|
||||
int: -300
|
||||
reflect type: int16
|
||||
int: 300
|
||||
reflect type: uint16
|
||||
uint: 50000
|
||||
reflect type: int32
|
||||
int: 7340032
|
||||
reflect type: int32
|
||||
int: -7340032
|
||||
reflect type: uint32
|
||||
uint: 7340032
|
||||
reflect type: int64
|
||||
int: 9895604649984
|
||||
reflect type: int64
|
||||
int: -9895604649984
|
||||
reflect type: uint64
|
||||
uint: 9895604649984
|
||||
reflect type: uintptr
|
||||
uint: 12345
|
||||
reflect type: float32
|
||||
float: +3.140000e+000
|
||||
reflect type: float64
|
||||
float: +3.140000e+000
|
||||
reflect type: complex64
|
||||
complex: (+1.200000e+000+3.000000e-001i)
|
||||
reflect type: complex128
|
||||
complex: (+1.300000e+000+4.000000e-001i)
|
||||
reflect type: string
|
||||
string: foo
|
||||
reflect type: unsafe.Pointer
|
||||
pointer: true
|
||||
|
||||
Reference in New Issue
Block a user