Signed<Magnitude>
The final thing I want to investigate before committing to a flexible-width solution is whether I can efficiently model the signed part as a Signed<Magnitude> wrapper. I know fixed-width is possible, but that's different.
- NBKFlexibleWidthKit
- UIntXL
- NBKSignedKit
- SInt, Signed<UInt>
- SIntXL, Signed<UIntXL>
Subquestion: if it's not possible, which set of changes would enable it?
Making Signed<Magnitude> super light-weight, with no 2's complement and no fixed-width, is also an option.
More on this: I could introduce a non-binary NBKInteger protocol, that's like Swift.Numeric + division + words.
I do love the light-weight approach. It's a high-value-low-maintenance coffee break project.
I would need some composable magnitude text conversion methods, but that's easy to add.
I'll need to make some changes, but it seems viable. I like it because it doesn't try to be something it is not, meaning it plays to its strengths and not to its weaknesses. It's much simpler that way and way easier to maintain. It also leaves a spot open for a two's complement solution, should I find the time or need it (#44).
~~I just realized something neat. Since these modules all live within the same package, I can use #if canImport without adding any target dependencies.~~ Edit: never-mind, I was fooled by Xcode :disappointed:
// in NBKSignedKit/NBKSigned.swift
#if canImport(NBKFlexibleWidthKit)
import NBKFlexibleWidthKit
public typealias SIntXL = NBKSigned<UIntXL>
#endif
I would need some composable magnitude text conversion methods, but that's easy to add.
Sign & magnitude encoding can be exposed in TextKit (#86), so I won't need another protocol method for that.
Conforming this model to Strideable is a bit of a headache at the moment, so if that's desirable I might have to add some constraints. All core integers use Int strides, so maybe that's the way to go. Or Magnitude.Stride, perhaps?
Hm. I kind of feel like this is teetering on the edge of over-engineering. Maybe I'll add a concrete sign-magnitude model to NBKFlexibleWidthKit and drop NBKSigned<Magnitude>. Dunno, I'll decide when I get to it.
There's only two interesting specializations of it: UInt and UIntXL.