Skip to content

[stdlib] Conform fixed-width integer types to LosslessStringConvertible #11193

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

Merged
merged 5 commits into from
Jul 29, 2017
Merged

[stdlib] Conform fixed-width integer types to LosslessStringConvertible #11193

merged 5 commits into from
Jul 29, 2017

Conversation

xwu
Copy link
Collaborator

@xwu xwu commented Jul 26, 2017

As approved in SE-0089, Int and its friends should conform to LosslessStringConvertible.

Since init?(_:radix:) is now implemented on FixedWidthInteger, this PR adopts the following design:

  1. FixedWidthInteger now refines LosslessStringConvertible
  2. The required initializer init?(_:) has a default implementation that calls init?(_:radix:).

There's clearly a test that can be added to test this conformance. What's going to be even more critical is assuring source compatibility.

Note: Currently, a method foo(x: Int, y: Int = 42) cannot satisfy a protocol requirement for foo(x: Int); however, both can co-exist without the compiler complaining about ambiguity.

///
/// - Parameter description: The ASCII representation of a number in base 10.
@_semantics("optimize.sil.specialize.generic.partial.never")
@inline(__always)
Copy link
Collaborator Author

@xwu xwu Jul 26, 2017

Choose a reason for hiding this comment

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

Are these appropriate and optimal uses of these internal attributes? I'm using the example of other initializers implemented in this file and Integers.swift.gyb.

Copy link
Contributor

Choose a reason for hiding this comment

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

/cc @swiftix for the usage of the @_semantics attribute.
@inline(__always) is fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

/cc @natecook1000 for the doc comment.

Copy link
Member

Choose a reason for hiding this comment

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

Can you include an out of range example? e.g.:

Int("10000000000000000000000000")               // Out of range

You could remove "in base 10" from the last paragraph, too.

@xwu
Copy link
Collaborator Author

xwu commented Jul 26, 2017

I'm unable to test this locally, as the ModelIO overlay is currently refusing to build. Can we do a test?

@cardoso
Copy link

cardoso commented Jul 26, 2017

+1

This PR fixes: SR-5558

@huonw
Copy link
Contributor

huonw commented Jul 26, 2017

@swift-ci Please smoke test.

I'm unable to test this locally, as the ModelIO overlay is currently refusing to build.

The work-around some of us are using is commenting out the contents of ModelIO.swift, since nothing uses it.

@huonw
Copy link
Contributor

huonw commented Jul 26, 2017

@swift-ci Please smoke test

@huonw huonw requested a review from moiseev July 26, 2017 21:24
@moiseev
Copy link
Contributor

moiseev commented Jul 26, 2017

@swift-ci Please Test Source Compatibility

@moiseev
Copy link
Contributor

moiseev commented Jul 26, 2017

@xwu Thanks!

There's clearly a test that can be added to test this conformance.

Is there a reason you haven't implemented a test as part of this PR?

@moiseev moiseev changed the title Conform fixed-width integer types to LosslessStringConvertible [stdlib] Conform fixed-width integer types to LosslessStringConvertible Jul 26, 2017
Copy link
Member

@natecook1000 natecook1000 left a comment

Choose a reason for hiding this comment

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

Thanks @xwu — a couple minor requests.

///
/// - Parameter description: The ASCII representation of a number in base 10.
@_semantics("optimize.sil.specialize.generic.partial.never")
@inline(__always)
Copy link
Member

Choose a reason for hiding this comment

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

Can you include an out of range example? e.g.:

Int("10000000000000000000000000")               // Out of range

You could remove "in base 10" from the last paragraph, too.

@@ -102,7 +102,7 @@ extension FixedWidthInteger {
///
/// The string passed as `text` may begin with a plus or minus sign character
/// (`+` or `-`), followed by one or more numeric digits (`0-9`) or letters
/// (`a-z` or `A-Z`). The string is case insensitive.
/// (`a-z` or `A-Z`). The string is case-insensitive.
Copy link
Member

Choose a reason for hiding this comment

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

This is correct as is. "The case-insensitive string…" vs "The string is case insensitive."

Copy link
Member

Choose a reason for hiding this comment

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

Okay, really the string is just a string. 😅 Can you change this to "The string parsing is case insensitive"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@natecook1000 You're right! My personal style is to hyphenate most compound adjectives (with the most common exception arising when the first word is an adverb that ends in "-ly"). However, Chicago Manual rules (which, I believe, is or was at one point Apple's house style) calls for the spelling "case insensitive" when it follows the noun.

Also an astute point about what exactly is case-insensitive. I'd tweak it a little and write: "Parsing of the string is case insensitive."

Copy link
Member

Choose a reason for hiding this comment

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

Sounds great!

@moiseev
Copy link
Contributor

moiseev commented Jul 27, 2017

Somewhat related to #11157

@xwu
Copy link
Collaborator Author

xwu commented Jul 27, 2017

@moiseev Yes, there was a very good reason: it was bedtime last night and I had fifteen hours of patient care duties today :) But now I've added one.

}

let i: [Int] = _toArray("1 2 3")
expectEqual(i.count, 3)
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI: there exists 'expectEqualSequence` https://github.com/apple/swift/blob/master/stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb#L2326.

So one can write checks like these in one shot:

expectEqualSequence([1, 2, 3], _toArray("1 2 3"))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's delightful.

@moiseev
Copy link
Contributor

moiseev commented Jul 27, 2017

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Jenkins build - Swift Test Linux Platform
Git Commit - f817803
Test requested by - @moiseev

@xwu
Copy link
Collaborator Author

xwu commented Jul 27, 2017

@moiseev Sorry, let's try this again; a minor change was necessary.

@swift-ci
Copy link
Contributor

Build failed
Jenkins build - Swift Test OS X Platform
Git Commit - f817803
Test requested by - @moiseev

@moiseev
Copy link
Contributor

moiseev commented Jul 28, 2017

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Jenkins build - Swift Test Linux Platform
Git Commit - 14a7c78
Test requested by - @moiseev

@swift-ci
Copy link
Contributor

Build failed
Jenkins build - Swift Test OS X Platform
Git Commit - 14a7c78
Test requested by - @moiseev

@moiseev
Copy link
Contributor

moiseev commented Jul 28, 2017

@swift-ci Please Test Source Compatibility

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.

6 participants