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

init?(_:) vs init(_:) #135

Open
oscbyspro opened this issue Nov 20, 2024 · 2 comments
Open

init?(_:) vs init(_:) #135

oscbyspro opened this issue Nov 20, 2024 · 2 comments
Labels
addition oh, so shiny! brrr such code, much wow maybe to do, or not to do? subtraction huh? where did it go?

Comments

@oscbyspro
Copy link
Owner

oscbyspro commented Nov 20, 2024

BinaryInteger initializers of the form T/init(_:) are abbreviations of T.exactly(_:).unwrap(). Alternatively, I could return Optional<T>. Optional<T> force-unwrapping is only one additional character ("!") and explicit error-handling is awesome. In this hypothetical rework, init?(_:) would an abbreviation of T.exactly(_:).optional().

@oscbyspro oscbyspro added addition oh, so shiny! brrr such code, much wow maybe to do, or not to do? subtraction huh? where did it go? labels Nov 20, 2024
@oscbyspro
Copy link
Owner Author

oscbyspro commented Dec 8, 2024

The more I think about it, the more I like this approach. It's easier to test since it doesn't trap. In the case of Guarantee, it would also result in fewer methods since error-handling is hoisted to Optional<T>. If I add Optional/prune(_:) then the number of Guarantee initializers would go from 5 to 2. Depending on how well LLVM optimizes unchecked(_:) it might even be possible to remove init(unsafe:).

init(_:)         // error: nil
init(unsafe:)    // error: %%%%%%
init(_:prune:)   // error: throws
init(exactly:)   // error: nil
init(_:)         // error: precondition
init(unchecked:) // error: assert
init(unsafe:)    // error: %%%%%%

@oscbyspro
Copy link
Owner Author

Also, if the Guarantee protocol becomes that simple, then I can probably drop it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition oh, so shiny! brrr such code, much wow maybe to do, or not to do? subtraction huh? where did it go?
Projects
None yet
Development

No branches or pull requests

1 participant