[stdlib] [SE-0067] Implement generic conversions to floating point #13782
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.
The following initializers, approved in SE-0067, are absent from currently shipping floating-point protocols
In this PR, the missing initializers are added to their respective protocols, adjusted for revised integer protocol names and furnished with updated documentation. Default implementations for all of the above initializers are provided for
BinaryFloatingPoint
, and tests for those default implementations are included.Some considerations
The default implementations provided here are not unnecessarily inefficient, but they are orders of magnitude slower than non-generic conversion initializers. For performance (and backwards compatibility) reasons, the non-generic conversion initializer requirements are maintained.
No default implementation is provided for
init<T : BinaryInteger>(_: T)
orinit?<T : BinaryInteger>(exactly: T)
onFloatingPoint
itself. They are technically possible to implement (by serial division of the magnitude by powers ofradix
, callinginit(_: UInt64)
at each iteration), but they would be very slow. Although technically source-breaking without such default implementations, no first-party type conforms toFloatingPoint
but notBinaryFloatingPoint
(Foundation.Decimal
does not conform toFloatingPoint
), and no Swift decimal types are found among searchable open-source projects. Any hypothetical decimal type that conforms toFloatingPoint
today is already required to implement the logic necessary to convert from binary integers, and efficient implementation of the new requirement would therefore be trivial.Heterogeneous comparison methods approved in SE-0067 and still commented out have not been added in this PR. It could be added in a distinct PR, but such a move may or may not be wise at the moment. First, implicit generic conversions will incur the performance penalty discussed above. Second, the issue remains unresolved regarding comparison to a literal in generic contexts defaulting to heterogeneous comparison. Third, as noted in the commented-out code, the implementation of these heterogeneous comparison methods is not fully correct, although it is serviceable for built-in types.