Skip to content

Differentiate throwing and non-throwing TestPairs #3

Description

@ZevEisenberg

The current implementation of TestPair can be initialized with a throwing or non-throwing autoclosure, but the getters for left and right can't know which, so they always have to assume non-throwing and force-try.

Here's some code from @IanKeen that might help:

public enum Throwing { }
public enum NonThrowing { }

public struct TestPair<Left, Right, Mode> {
    private let _leftClosure: () throws -> Left
    private let _rightClosure: () throws -> Right
}

extension TestPair where Mode == NonThrowing {
    public init(leftClosure: @escaping () -> Left, rightClosure: @escaping () -> Right) {
        self.init(
            _leftClosure: leftClosure,
            _rightClosure: rightClosure
        )
    }

    public var left: Left {
      get { try! _leftClosure() }
    }
    public var right: Right {
      get { try! _rightClosure() }
    }
}
extension TestPair where Mode == Throwing {
    init(leftClosure: @escaping () throws -> Left, rightClosure: @escaping () throws -> Right) {
        self.init(
            _leftClosure: leftClosure,
            _rightClosure: rightClosure
        )
    }
    public var left: Left {
      get throws { try _leftClosure() }
    }
    public var right: Right {
      get throws { try _rightClosure() }
    }
}

Usage:
image

Further ramblings from me:

might do protocol TestMode {} and then enum Throwing: TestMode so I can constraint TestMode in the definition of TestPair

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions