aboutsummaryrefslogtreecommitdiff
path: root/src/reflect/value.go
diff options
context:
space:
mode:
authorFabio Falzoi <fabio.falzoi84@gmail.com>2021-09-25 18:44:24 +0200
committerIan Lance Taylor <iant@golang.org>2021-09-27 21:31:14 +0000
commit04f7521b0aef10d523d6de006e723afd349b4b95 (patch)
tree1f24febcd49b01f83cfa091d2d4a3e8eb19ac005 /src/reflect/value.go
parent8e34d779578736942c78f9857f5bb9b9025815c6 (diff)
downloadgo-04f7521b0aef10d523d6de006e723afd349b4b95.tar.gz
go-04f7521b0aef10d523d6de006e723afd349b4b95.zip
reflect: add Value.{CanInt, CanUint, CanFloat, CanComplex}
As discussed in #47658, Value already has CanAddr and CanInterface to test if a call to Addr or Inteface, respectively, does not result in a panic. Therefore we add CanInt, CanUint, CanFloat and CanComplex to ease the test for a possible panic in calling, respectively, Int, Uint, Float and Complex. Fixes #47658 Change-Id: I58b77d77e6eec9f34234e985f631eab72b5b935e Reviewed-on: https://go-review.googlesource.com/c/go/+/352131 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Trust: David Chase <drchase@google.com> Trust: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/reflect/value.go')
-rw-r--r--src/reflect/value.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go
index 786c494166..39b82a8c01 100644
--- a/src/reflect/value.go
+++ b/src/reflect/value.go
@@ -1122,6 +1122,16 @@ func (v Value) Close() {
chanclose(v.pointer())
}
+// CanComplex reports whether Complex can be used without panicking.
+func (v Value) CanComplex() bool {
+ switch v.kind() {
+ case Complex64, Complex128:
+ return true
+ default:
+ return false
+ }
+}
+
// Complex returns v's underlying value, as a complex128.
// It panics if v's Kind is not Complex64 or Complex128
func (v Value) Complex() complex128 {
@@ -1249,6 +1259,16 @@ func (v Value) FieldByNameFunc(match func(string) bool) Value {
return Value{}
}
+// CanFloat reports whether Float can be used without panicking.
+func (v Value) CanFloat() bool {
+ switch v.kind() {
+ case Float32, Float64:
+ return true
+ default:
+ return false
+ }
+}
+
// Float returns v's underlying value, as a float64.
// It panics if v's Kind is not Float32 or Float64
func (v Value) Float() float64 {
@@ -1310,6 +1330,16 @@ func (v Value) Index(i int) Value {
panic(&ValueError{"reflect.Value.Index", v.kind()})
}
+// CanInt reports whether Int can be used without panicking.
+func (v Value) CanInt() bool {
+ switch v.kind() {
+ case Int, Int8, Int16, Int32, Int64:
+ return true
+ default:
+ return false
+ }
+}
+
// Int returns v's underlying value, as an int64.
// It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64.
func (v Value) Int() int64 {
@@ -2391,6 +2421,16 @@ func (v Value) Type() Type {
return v.typ.typeOff(m.mtyp)
}
+// CanUint reports whether Uint can be used without panicking.
+func (v Value) CanUint() bool {
+ switch v.kind() {
+ case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
+ return true
+ default:
+ return false
+ }
+}
+
// Uint returns v's underlying value, as a uint64.
// It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
func (v Value) Uint() uint64 {