Skip to content

Commit

Permalink
Merge pull request #253 from antitypical/make-try-functions-unavailable
Browse files Browse the repository at this point in the history
[4.0] Make `try` functions unavailable
  • Loading branch information
ikesyo authored Oct 6, 2017
2 parents fb2b67e + b361006 commit 7e1eb89
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 79 deletions.
52 changes: 24 additions & 28 deletions Result/Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,34 +143,6 @@ public func materialize<T>(_ f: @autoclosure () throws -> T) -> Result<T, AnyErr
return Result(f)
}

// MARK: - Cocoa API conveniences

#if !os(Linux)

/// Constructs a `Result` with the result of calling `try` with an error pointer.
///
/// This is convenient for wrapping Cocoa API which returns an object or `nil` + an error, by reference. e.g.:
///
/// Result.try { NSData(contentsOfURL: URL, options: .dataReadingMapped, error: $0) }
public func `try`<T>(_ function: String = #function, file: String = #file, line: Int = #line, `try`: (NSErrorPointer) -> T?) -> Result<T, NSError> {
var error: NSError?
return `try`(&error).map(Result.success) ?? .failure(error ?? Result<T, NSError>.error(function: function, file: file, line: line))
}

/// Constructs a `Result` with the result of calling `try` with an error pointer.
///
/// This is convenient for wrapping Cocoa API which returns a `Bool` + an error, by reference. e.g.:
///
/// Result.try { NSFileManager.defaultManager().removeItemAtURL(URL, error: $0) }
public func `try`(_ function: String = #function, file: String = #file, line: Int = #line, `try`: (NSErrorPointer) -> Bool) -> Result<(), NSError> {
var error: NSError?
return `try`(&error) ?
.success(())
: .failure(error ?? Result<(), NSError>.error(function: function, file: file, line: line))
}

#endif

// MARK: - ErrorConvertible conformance

extension NSError: ErrorConvertible {
Expand All @@ -195,6 +167,30 @@ public func materialize<T>(_ f: @autoclosure () throws -> T) -> Result<T, NSErro
fatalError()
}

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)

/// Constructs a `Result` with the result of calling `try` with an error pointer.
///
/// This is convenient for wrapping Cocoa API which returns an object or `nil` + an error, by reference. e.g.:
///
/// Result.try { NSData(contentsOfURL: URL, options: .dataReadingMapped, error: $0) }
@available(*, unavailable, message: "This has been removed. Use `Result.init(attempt:)` instead. See https://github.com/antitypical/Result/issues/85 for the details.")
public func `try`<T>(_ function: String = #function, file: String = #file, line: Int = #line, `try`: (NSErrorPointer) -> T?) -> Result<T, NSError> {
fatalError()
}

/// Constructs a `Result` with the result of calling `try` with an error pointer.
///
/// This is convenient for wrapping Cocoa API which returns a `Bool` + an error, by reference. e.g.:
///
/// Result.try { NSFileManager.defaultManager().removeItemAtURL(URL, error: $0) }
@available(*, unavailable, message: "This has been removed. Use `Result.init(attempt:)` instead. See https://github.com/antitypical/Result/issues/85 for the details.")
public func `try`(_ function: String = #function, file: String = #file, line: Int = #line, `try`: (NSErrorPointer) -> Bool) -> Result<(), NSError> {
fatalError()
}

#endif

// MARK: -

import Foundation
51 changes: 0 additions & 51 deletions Tests/ResultTests/ResultTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,36 +179,6 @@ final class ResultTests: XCTestCase {
XCTAssertEqual(left.recover(with: right).error, .right)
}

// MARK: Cocoa API idioms

#if !os(Linux)

func testTryProducesFailuresForBooleanAPIWithErrorReturnedByReference() {
let result = `try` { attempt(true, succeed: false, error: $0) }
XCTAssertFalse(result ?? false)
XCTAssertNotNil(result.error)
}

func testTryProducesFailuresForOptionalWithErrorReturnedByReference() {
let result = `try` { attempt(1, succeed: false, error: $0) }
XCTAssertEqual(result ?? 0, 0)
XCTAssertNotNil(result.error)
}

func testTryProducesSuccessesForBooleanAPI() {
let result = `try` { attempt(true, succeed: true, error: $0) }
XCTAssertTrue(result ?? false)
XCTAssertNil(result.error)
}

func testTryProducesSuccessesForOptionalAPI() {
let result = `try` { attempt(1, succeed: true, error: $0) }
XCTAssertEqual(result ?? 0, 1)
XCTAssertNil(result.error)
}

#endif

func testTryMapProducesSuccess() {
let result = success.tryMap(tryIsSuccess)
XCTAssert(result == success)
Expand Down Expand Up @@ -259,19 +229,6 @@ extension AnyError: Equatable {
}
}

#if !os(Linux)

func attempt<T>(_ value: T, succeed: Bool, error: NSErrorPointer) -> T? {
if succeed {
return value
} else {
error?.pointee = Result<(), NSError>.error()
return nil
}
}

#endif

func tryIsSuccess(_ text: String?) throws -> String {
guard let text = text, text == "success" else {
throw error
Expand All @@ -294,8 +251,6 @@ extension NSError {
}
}

#if os(Linux)

extension ResultTests {
static var allTests: [(String, (ResultTests) -> () throws -> Void)] {
return [
Expand All @@ -320,10 +275,6 @@ extension ResultTests {
("testRecoverWithProducesLeftForLeftSuccess", testRecoverWithProducesLeftForLeftSuccess),
("testRecoverWithProducesRightSuccessForLeftFailureAndRightSuccess", testRecoverWithProducesRightSuccessForLeftFailureAndRightSuccess),
("testRecoverWithProducesRightFailureForLeftFailureAndRightFailure", testRecoverWithProducesRightFailureForLeftFailureAndRightFailure),
// ("testTryProducesFailuresForBooleanAPIWithErrorReturnedByReference", testTryProducesFailuresForBooleanAPIWithErrorReturnedByReference),
// ("testTryProducesFailuresForOptionalWithErrorReturnedByReference", testTryProducesFailuresForOptionalWithErrorReturnedByReference),
// ("testTryProducesSuccessesForBooleanAPI", testTryProducesSuccessesForBooleanAPI),
// ("testTryProducesSuccessesForOptionalAPI", testTryProducesSuccessesForOptionalAPI),
("testTryMapProducesSuccess", testTryMapProducesSuccess),
("testTryMapProducesFailure", testTryMapProducesFailure),
("testAnyErrorDelegatesLocalizedDescriptionToUnderlyingError", testAnyErrorDelegatesLocalizedDescriptionToUnderlyingError),
Expand All @@ -334,8 +285,6 @@ extension ResultTests {
}
}

#endif

import Foundation
import Result
import XCTest

0 comments on commit 7e1eb89

Please sign in to comment.