-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: reflect: add Overflows method on reflect.Value #46772
Comments
Perhaps this should not be in reflect, as the concept is important and often relevant to tight code. |
See also #46746. |
You suggest checking for loss of precision. What should happen when converting |
The usual Go idiom for this is doing a round trip conversion and then checking for equality: (That is for concrete values. For types, you'd define it whether the round trip succeeds for all possible values.) |
That doesn't catch cases where the semantic value has changed:
I think part of the challenge with this API is that people are going to reasonably disagree about what is considered an "overflow" or "loss of precision". |
That's a good question. I would lean towards returning false. I agree that Alternatively, we could add a new method on For example:
That gets a little trickier for things like string("5") and int(5), which I would argue should return false, though at least that wouldn't pass |
I'm going to say basically the same thing here that I said on #48218. To me, The existing In contrast, I think that the I do think that the proposed overflow-check function would be a useful library — I just don't think it belongs in the |
This proposal has been added to the active column of the proposals project |
It sounds like there are enough subtleties here that we should probably not add this. Do I have that right? |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
The proposal is to add
reflect.Value.Overflows(reflect.Type) bool
to the reflect package. This method onreflect.Value
types would basically be a more strict version ofreflect.Type.ConvertibleTo(reflect.Type)
, returning false if an overflow or precision loss would occur when convertingreflect.Value
to the givenreflect.Type
.For example:
or
Why is
reflect.Type.ConvertibleTo(reflect.Type)
not enough?reflect.Type.ConvertibleTo(reflect.Type)
is helpful in understanding whether a type can be converted to another, but the value is required to know whether there will be precision loss or overflow. It's also been noted that ConvertibleTo alone can't be used to determine whether a type is safe to convert to another, asreflect.Value.Convert(reflect.Type)
can still panic in some circumstances.The real world use case came up during discussion of https://golang.org/cl/325702
credit to @rolandshoemaker who came up with the API
The text was updated successfully, but these errors were encountered: