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

Add: BinaryInteger/negate() #80

Closed
oscbyspro opened this issue Sep 2, 2024 · 2 comments
Closed

Add: BinaryInteger/negate() #80

oscbyspro opened this issue Sep 2, 2024 · 2 comments
Labels
addition oh, so shiny!

Comments

@oscbyspro
Copy link
Owner

oscbyspro commented Sep 2, 2024

The result of BinaryInteger/negated() is often assigned to self so having a mutating convenience for it might be nice.

x.negate().discard() // with wrapping behavior
x.negate().unchecked("this can't fail because...")
extension BinaryInteger {

    /// Forms the additive inverse of `self` and an `error` indicator.
    @inlinable public mutating func negate() -> Fallible<Void> {
        let error: Bool
        (self, error) = self.negated().components()
        return Fallible(Void(), error: error)
    }
}
@oscbyspro oscbyspro added the addition oh, so shiny! label Sep 2, 2024
@oscbyspro
Copy link
Owner Author

oscbyspro commented Sep 2, 2024

I don't want too many mutating conveniences but I think negate() is in the same class as toggle(), mainly because a binary integer negate() is a fallible sign.toggle() in sign-and-magnitude algorithms.

@oscbyspro
Copy link
Owner Author

I also don't see much point in adding a Fallible/negate() because mutating is already an opt-out of the recovery chain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition oh, so shiny!
Projects
None yet
Development

No branches or pull requests

1 participant