Make Numeric
convenience properties more strict in their type conversions
#62
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request has arisen from my work on what is now an alternative implementation to pull request #61. I realized the need for more precision in the decoding process of integers (signed and unsigned) and floating point numbers. This patch does not modify the packing or unpacking process. It only provides a more robust and specific set of convenience properties for each width of integer, float, or double, requiring the value be exactly representable by the target type if the packed value is not already of the correct type.
I believe this approach might result in less decoding errors. For example, previously, if you have a Double precision float point number, and accidentally invoke
MessagePackValue.floatValue
you would get back a nicely truncated float. This loss of precision could easily go unnoticed. Instead,MessagePackValue.floatValue
will only return non-nil if the underlyingDouble
can truly be represented by a float (in many cases, it cannot). While this might seem somewhat inconvenient initially, it offers a nice forcing function to ensure the correct data type is being decoded earlier in the development process. If one is okay with loss of precision, a simple truncating is no more difficult (but entirely more clear to the reader) than:I think this stricter typing in the convenience property API is more in line with the strict typing promoted by swift itself.