Skip to content

Commit

Permalink
Merge pull request #214 from antitypical/swift-3.1-linux-compatibility
Browse files Browse the repository at this point in the history
[PoC] Modify `materialize` overload of `Result<T, NSError>` to be buildable on both Swift 3.0 and 3.1
  • Loading branch information
mdiep authored Mar 3, 2017
2 parents 5db7bc7 + 939c0f3 commit 7df607a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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
}
}

Expand Down

0 comments on commit 7df607a

Please sign in to comment.