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

[PoC] Modify materialize overload of Result<T, NSError> to be buildable on both Swift 3.0 and 3.1 #214

Merged
merged 3 commits into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,16 @@ matrix:
language: generic
install:
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)"
- script:
- swift build
- swift test
env:
- JOB=Linux
- SWIFT_VERSION=3.1-DEVELOPMENT-SNAPSHOT-2017-03-01-a
sudo: required
dist: trusty
language: generic
install:
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)"
notifications:
email: false
13 changes: 11 additions & 2 deletions Result/Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,17 @@ public func materialize<T>(_ f: () throws -> T) -> Result<T, NSError> {
public func materialize<T>(_ f: @autoclosure () throws -> T) -> Result<T, NSError> {
do {
return .success(try f())
} catch let error as NSError {
return .failure(error)
} catch {
// This isn't great, but it lets us maintain compatibility until this deprecated
// method can be removed.
#if _runtime(_ObjC)
return .failure(error as NSError)
#else
// https://github.com/apple/swift-corelibs-foundation/blob/swift-3.0.2-RELEASE/Foundation/NSError.swift#L314
let userInfo = _swift_Foundation_getErrorDefaultUserInfo(error) as? [String: Any]
let nsError = NSError(domain: error._domain, code: error._code, userInfo: userInfo)
return .failure(nsError)
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is acceptable, but would prefer to see a comment that says something like "This isn't great, but it lets us maintain compatibility until this deprecated method can be removed."

}
}

Expand Down