oscbyspro / Ultimathnum

Binary arithmetic reimagined in Swift
Apache License 2.0
7 stars 1 forks source link

Rework: Recoverable #54

Closed oscbyspro closed 1 month ago

oscbyspro commented 1 month ago

I want to solve a boilerplate problem. I imagine something like the following would be nice. I already have two approaches in mind. One is proper, but a bit awkward. The other is straightforward, but relies on heroic constant-folding. I'll have to look at the assembly.

extension Recoverable where Payload: BinaryInteger {
    func combine(_ other: some Recoverable<Payload>) -> Fallible<Payload> { ... }
}
oscbyspro commented 1 month ago

The simple approach seems viable, as far as I can tell:

protocol Recoverable<Value> {

    associatedtype Value = Self

    var error: Bool  { borrowing get }

    var value: Value { borrowing get }
}

I could require static functions, but this is also an option:

extension BinaryInteger: Recoverable {

    @available(*, deprecated, message: "It always returns: false.")
    var error: Bool { false }

    @available(*, deprecated, message: "It always returns: self.")
    var value: Self { self  }
}
oscbyspro commented 1 month ago

Seems great, for the most part, but integer literals need explicit types because of some Recoverable<Value>.

oscbyspro commented 1 month ago

I suppose it is a kind of boilerplate that could also be solved by a macro. It might be something to think about.

oscbyspro commented 1 month ago

Hm. I might have to go with the fancier approach so that Recoverable/remainder(_:) may return Self, for example.

oscbyspro commented 1 month ago

I'm tempted by the macro approach, because solving a boilerplate problem doesn't seem that valuable on its own. But, I think the protocol approach might still be correct because it is a step towards (#46).

oscbyspro commented 1 month ago

Hm. I wonder how to model IXL/incremented():

U32/incremented() -> Fallible<U32>
IXL/incremented() -> IXL // huh?
Fallible<U32>/incremented() -> Fallible<U32>
Fallible<IXL>/incremented() -> Fallible<IXL>
oscbyspro commented 1 month ago

It could also be that this isn't important. It might be simpler to just drop this protocol. Hm.