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

Differentiate throwing and non-throwing TestPairs #3

Open
ZevEisenberg opened this issue May 29, 2023 · 1 comment · May be fixed by #4
Open

Differentiate throwing and non-throwing TestPairs #3

ZevEisenberg opened this issue May 29, 2023 · 1 comment · May be fixed by #4
Assignees

Comments

@ZevEisenberg
Copy link
Owner

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

@ZevEisenberg ZevEisenberg self-assigned this May 29, 2023
@ZevEisenberg
Copy link
Owner Author

Considered calling the protocol GameOfThrows but narrowly managed to resist.

@ZevEisenberg ZevEisenberg linked a pull request Jul 15, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant