oscbyspro / Ultimathnum

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

Improve error handling with Optional<Fallible<T>> #95

Closed oscbyspro closed 1 month ago

oscbyspro commented 2 months ago

BinaryInteger.leniently(Swift.BinaryFloatingPoint) is an excellent use case for Optional<Fallible<T>>. The nil and error both have distinct and sensible interpretations and the optional part doesn't even increase the size of the return type's memory layout.

I have previously hoisted some Fallible<T> return types by mapping errors to Optional<T>. I think it's worth considering Optional<Fallible<T>> as an alternative because it is more powerful. Tangentially, I could perhaps do something similar with infinite-by-finite division (#69).

oscbyspro commented 1 month ago

I'll make BinaryInteger/factorial() return Optional<Fallible<Self>> and add a similar Fallible<T> method.

oscbyspro commented 1 month ago

Note that the optional part doesn't affect the assembly as long as you unsafelyUnwrapped and that many binary integer algorithms are such that some types can unsafelyUnwrapped unconditionally. Alternatively, you can use the Guarantee approach when you have more information than the compiler.