Skip to content
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

Make Numeric convenience properties more strict in their type conversions #62

Merged
merged 1 commit into from
Feb 22, 2018

Conversation

mgadda
Copy link
Contributor

@mgadda mgadda commented Oct 9, 2017

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 underlying Double 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:

var value: MessagePackValue
Float(value.doubleValue)

I think this stricter typing in the convenience property API is more in line with the strict typing promoted by swift itself.

Copy link

@Bersaelor Bersaelor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this PR, definitely would help with the use-cases I had in mind for MessagePack.
Also kudos for going the extra mile with the Unit Tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants