-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reflect: Overflow methods should be on Type #60427
Comments
It may also be implemented as a single generic function:
and used in this way:
|
@gazerro I'd prefer |
Oh, also, I don't think that API is actually sufficiently flexible. It wouldn't allow you to check if a |
It can simply be written as But that doesn't solve the problem mentioned in this proposal, which is about checking for overflow using a |
@gazerro To be clear: I was suggesting making that a method on I think there are a couple of bikeshed colors (say
The last three all have the advantage of minimizing API surface - they only add one exported function/method. The last one has the advantage that it makes the call look uniform and requires the least boilerplate no matter if you have a Number 2 could be combined with 1 as well, to give Also, while we bikeshed, I think The JSON example from the proposal text notably packs the |
It looks great to me. The code in |
To add one more color of bikeshed: |
We need to be careful not to add any allocations or other cost. This call would need to happen every single time the JSON decoder encounters a number, for example. cc @dsnet |
There are no other methods of |
This seems like a fairly uncontroversial proposal - @ianlancetaylor do you think we could consider it for Go 1.22? I am more than happy to send the CL. |
I'll add it to the list to review soonish. |
This proposal has been added to the active column of the proposals project |
This seems straightforward. I believe the new API is:
Do I have that right? |
Based on the discussion above, this proposal seems like a likely accept. Adding to package reflect:
|
No change in consensus, so accepted. 🎉 Adding to package reflect:
|
It looks like there's a typo in the 4th method, fixable by applying the following diff: -func (t Value) OverflowUint(x uint64) bool
+func (t Type) OverflowUint(x uint64) bool The |
Change https://go.dev/cl/567296 mentions this issue: |
Change https://go.dev/cl/567775 mentions this issue: |
Change https://go.dev/cl/567917 mentions this issue: |
CL 567296 added {OverflowComplex, OverflowFloat, OverflowInt, OverflowUint} to reflect.Type, this CL uses these methods to simplify code. For #60427 Change-Id: I229aef9e4095a2f025afd782081f6c9e6d7710f3 GitHub-Last-Rev: c824e5a GitHub-Pull-Request: #66000 Reviewed-on: https://go-review.googlesource.com/c/go/+/567775 Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Joseph Tsai <joetsai@digital-static.net> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
For consistency, this CL cleans up unnecessary comments, and moves these Overflow methods to exported area. For #60427 Change-Id: I14d4ffbc3552d31c211ea1e0b7a0f7090a4a8b89 GitHub-Last-Rev: acdc6ad GitHub-Pull-Request: #66019 Reviewed-on: https://go-review.googlesource.com/c/go/+/567917 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
See these four methods on
reflect.Value
:For example, looking at
OverflowInt
's godoc:None of that needs a value, in my opinion - a type would suffice. The implementation seems to confirm that - only the "kind" and "type" are used, not the actual value.
So I think the methods should be added to
Type
, with the same signature.I noticed this because quite a few projects have code like
reflect.Zero(typ).OverflowInt(x)
, when in fact it could just betyp.OverflowInt(x)
, saving the creation of a new value. For example, inencoding/json
: https://cs.opensource.google/go/go/+/refs/tags/go1.20.4:src/encoding/json/decode.go;l=799As for users who have a
reflect.Value
rather than areflect.Type
, they could also replaceval.OverflowInt(x)
withval.Type().OverflowInt(x)
, and we could consider deprecating theValue
methods in favor of theType
methods after a few Go releases. I don't personally see a point in keeping eight methods around;.Type()
isn't enough characters to warrant a shortcut, and in my experience, most reflect users will need to get thereflect.Type
for other reasons.The text was updated successfully, but these errors were encountered: