Skip to content

Conversation

@jshier
Copy link
Contributor

@jshier jshier commented Dec 3, 2018

This PR adds the updated Result implementation for SE-0235, as I broke the last PR when trying to rebase off of @rjmccall's Error self conformance PR. Previous PR discussion and review was here.

This PR includes #20629, as Error self conformance is required for this implementation of Result.

@jshier jshier mentioned this pull request Dec 3, 2018
@jshier
Copy link
Contributor Author

jshier commented Dec 3, 2018

I did run into some awkwardness on line 74 of the tests, where I couldn't use the property accessor for error (though the value property works fine.

@benrimmington
Copy link
Contributor

I copied the header from the other files. Should this one have an updated date?

It should be the year of first publication: Copyright (c) 2018 Apple Inc.

See SR-7507 and #14576 (comment).

return nil
}

var error: Error? {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is nested inside var value: Value?, which is why you aren't able to call result.error. IMO, it's better to just remove this extension and remove those tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed in 7fb5905. I'd like to leave the tests for now, as they're the only ones testing the unwrapping. I can remove the property extension if you think that's important.

@jshier
Copy link
Contributor Author

jshier commented Dec 4, 2018

@benrimmington @slavapestov Copyright year fixed in 7fb5905.

@lancep lancep requested a review from moiseev December 5, 2018 21:59
@moiseev
Copy link
Contributor

moiseev commented Dec 5, 2018

@natecook1000, do you mind looking at the doc comments?

@natecook1000 natecook1000 self-requested a review December 5, 2018 22:05
@swiftlang swiftlang deleted a comment from grand-china Dec 6, 2018
@moiseev
Copy link
Contributor

moiseev commented Dec 6, 2018

@swift-ci Please test

@moiseev
Copy link
Contributor

moiseev commented Dec 6, 2018

Now that #20629 has been merged, I would expect the diff to only contain stdlib changes...

@swift-ci
Copy link
Contributor

swift-ci commented Dec 6, 2018

Build failed
Swift Test Linux Platform
Git Sha - 7fb5905

expectEqual(newResult4, .failure(.err))
}

ResultTests.test("Equatable") {
Copy link
Contributor

@moiseev moiseev Dec 6, 2018

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@jshier jshier Dec 6, 2018

Choose a reason for hiding this comment

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

No, as I mostly cribbed these tests by looking at those from Optional and earlier feedback. I didn't know that existed. Can you provide an example of what they would simplify here?

In general, is there any documentation around this special StdlibUnittest API or testing requirements for the standard library?

Copy link
Contributor

@moiseev moiseev Dec 6, 2018

Choose a reason for hiding this comment

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

I was just thinking out loud really. These conformances are synthesized by the compiler, and should be working. Otherwise we're in trouble.

In general, is there any documentation around this special StdlibUnittest API or testing requirements for the standard library?

Unfortunately no.

Copy link
Contributor

@moiseev moiseev Dec 6, 2018

Choose a reason for hiding this comment

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

Can you provide an example of what they would simplify here?

It's not about simplification. Just that checkEquatable tests for the cases you and I might not necessarily think about, like whether == is transitive or not, or whether the implementation is correct in that == is the opposite of !=.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a use of checkEquatable, in additional to the tests that were already there. Let me know if there're any other scenarios you want tested, or if you feel the tests are now redundant.

Copy link
Member

Choose a reason for hiding this comment

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

Note: checkHashable starts by calling checkEquatable -- so it is okay to omit the checkEquatable test if we have tests elsewhere with checkHashable.

@moiseev
Copy link
Contributor

moiseev commented Dec 6, 2018

@jshier do you mind creating a separate PR for the swift-5.0-branch?

@moiseev
Copy link
Contributor

moiseev commented Dec 6, 2018

@swift-ci Please test

@jshier
Copy link
Contributor Author

jshier commented Dec 6, 2018

@jshier do you mind creating a separate PR for the swift-5.0-branch?

Sure. From this same branch to the swift-5.0-branch?

@benrimmington
Copy link
Contributor

#21073 isn't merged yet.

@moiseev
Copy link
Contributor

moiseev commented Dec 6, 2018

Sure. From this same branch to the swift-5.0-branch?

Just git cherry-pick -x your commits on top of swift-5.0-branch for a new PR.

#21073 isn't merged yet.

But when it lands, we will have a PR ready for CI :-)

@swift-ci
Copy link
Contributor

swift-ci commented Dec 6, 2018

Build failed
Swift Test Linux Platform
Git Sha - 2bc4cbb

@benrimmington
Copy link
Contributor

Now that #20629 has been merged, I would expect the diff to only contain stdlib changes...

Is it worth squashing the stdlib commits and rebasing from master?
Would this make it any easier to cherry pick for the 5.0 branch?

let result1: Result<Int, Err> = .success(1)
let result2: Result<Int, Err> = .success(2)
let result3: Result<Int, Err> = .failure(.err)
checkHashable([result1, result2, result3], equalityOracle: { $0 == $1 })
Copy link
Member

Choose a reason for hiding this comment

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

It would also be useful to have a hashing check with potentially colliding Success/Failure hash encodings:

Suggested change
checkHashable([result1, result2, result3], equalityOracle: { $0 == $1 })
checkHashable([result1, result2, result3], equalityOracle: { $0 == $1 })
let confusables: [Result<Err, Err>] = [
.success(.err)
.success(.derr)
.failure(.err)
.failure(.derr)
]
checkHashable(confusables, equalityOracle: { $0 == $1 })

checkHashable verifies that all four of these cases have different hash encodings. (The original implementation of hash(into:) had an issue with this, so it's not a theoretical concern.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

Copy link
Collaborator

@xwu xwu left a comment

Choose a reason for hiding this comment

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

Some comments on the documentation.

public init(catching body: () throws -> Success) {
do {
let value = try body()
self = .success(value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why split these lines instead of just

self = .success(try body())

? I'm curious, as that's something I do a lot in my code

Copy link
Collaborator

@xwu xwu left a comment

Choose a reason for hiding this comment

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

Some minor nits, but I like the changes to the documentation; @natecook1000, of course, is the guru and may have wiser thoughts.

///
/// - Parameter transform: A closure that takes the success value of the
/// instance.
/// - Returns: A`Result` instance, either from the closure or the previous
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// - Returns: A`Result` instance, either from the closure or the previous
/// - Returns: A `Result` instance, either from the closure or the previous

/// Get the success value as a throwing expression.
///
/// - Returns: The success value, if the instance is `.success`.
/// - Throws: The failure value, if the instance is `.failure`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// - Throws: The failure value, if the instance is `.failure`.
/// - Throws: The failure value, if the instance is `.failure`.


extension Result where Failure == Swift.Error {
/// Create an instance by evaluating a throwing closure, capturing the
/// returned value as a `.success` or any thrown error as `.failure`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// returned value as a `.success` or any thrown error as `.failure`.
/// returned value as a `.success` or any thrown error as a `.failure`.

Copy link
Contributor

@moiseev moiseev left a comment

Choose a reason for hiding this comment

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

:shipit:

@moiseev
Copy link
Contributor

moiseev commented Dec 10, 2018

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 80ae4de

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 80ae4de

@xwu xwu changed the title SE-0235: Add Result<Value, Error: Swift.Error> to Standard Library SE-0235: Add Result<Success, Failure: Error> to Standard Library Dec 10, 2018
@natecook1000 natecook1000 removed their request for review December 10, 2018 17:55
@moiseev
Copy link
Contributor

moiseev commented Dec 10, 2018

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - c739498

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - c739498

@moiseev
Copy link
Contributor

moiseev commented Dec 10, 2018

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - b167703

@moiseev
Copy link
Contributor

moiseev commented Dec 11, 2018

@swift-ci Please test source compatibility

@moiseev moiseev merged commit 83ccb23 into swiftlang:master Dec 12, 2018
@jshier
Copy link
Contributor Author

jshier commented Dec 12, 2018

@moiseev Should there be a CHANGELOG entry for this? Also, should I do a PR to update the proposal text to match the final implementation?

@moiseev
Copy link
Contributor

moiseev commented Dec 13, 2018

@jshier update to a proposal would be great. Don't know the policy for CHANGELOG. Let me find out.

UPD: Yes, please update the CHANGELOG as well.

@AnthonyLatsis AnthonyLatsis added swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented feature A feature request or implementation standard library Area: Standard library umbrella labels Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature A feature request or implementation standard library Area: Standard library umbrella swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented

Projects

None yet

Development

Successfully merging this pull request may close these issues.