-
Notifications
You must be signed in to change notification settings - Fork 1
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
Improved type testing #71
Comments
The type inference part would allow me to omit the "OrCrash" part from methods like: func _multipliedFullWidth(by amount: Self) -> DoubleWidth {
if High.Magnitude.self == Low.self {
return self._multipliedFullWidthAsKaratsubaAsDoubleWidthOrCrash(by: amount)
}
return self._multipliedFullWidthAsNormal(by: amount)
} func _multipliedFullWidthAsKaratsubaAsDoubleWidthOrCrash(by amount: Self) -> DoubleWidth {
assert(High.Magnitude.self == Low.self)
let negate: Bool = self.isLessThanZero != amount.isLessThanZero
let lhs = self.magnitude as! ANKFullWidth<Low, Low>
let rhs = amount.magnitude as! ANKFullWidth<Low, Low>
let product = lhs._multipliedFullWidthAsKaratsubaAsUnsigned(by: rhs) as! Magnitude.DoubleWidth
return DoubleWidth(bitPattern: negate ? product.twosComplement() : product)
} |
Another directly applicable use case would be improving methods like the following: init?(exactly source: some BinaryInteger) {
if let source = source as? Self {
self = source
return
}
if let source = source as? Int {
self.init(_exactlyAsDigit: source)
return
}
if let source = source as? UInt {
self.init(_exactlyAsDigit: source)
return
}
if let source = source as? Magnitude {
self.init(_exactlyAsMagnitude: source)
return
}
self.init(_exactlyAsBinaryInteger: source)
} |
It hasn't even been pitched yet, but I want the ability to add paths that are only evaluated when a function is specialized. This would be useful in cases where a type-checked fast path is slower than an unchecked slow path. It would also speed up the unspecialized slow path, which is otherwise slowed down by having to fail a bunch of type checks first. init?<T>(exactly source: T) where T: BinaryInteger {
// fast paths that only exist when specialized
if where(specialized) T == Self { ... }
if where(specialized) T == Int { ... }
if where(specialized) T == UInt { ... }
// only the slow path exists when unspecialized
self.init(_exactlyAsBinaryInteger: source)
} |
Joseph Groff mentioned a potential type testing feature:
The text was updated successfully, but these errors were encountered: