Using Xcode 10.0 beta (10L176w) on my macOS High Sierra system. Got this in a playground:
/// A multiple-precision negative-base integer type, with a radix of -8.
public struct NegaOctal: Hashable {
/// The type for each digit.
typealias Digit = Int4
/// A collection of all the digits.
typealias Digits = [Digit]
/// The digits of the represented value.
var digits: Digits = []
}
//...
extension NegaOctal {
/**
Generates a copy of this value in the given return type.
A runtime error may occur if intermediate or final results get too (absolutely) large. Even if a result is successfully returned, the value may be inaccurate if bits rolled into the sign bit (if any) or off the storage entirely.
- Parameter ofType: The return type.
- Returns: The converted value, if representable.
*/
public func toInt<T: BinaryInteger>(ofType: T.Type) -> T {
guard !digits.isEmpty else { return 0 }
let positiveComponentIndex = 0, negativeComponentIndex = 1
var components: [T] = [0, 0]
let componentIndex = digits.count % 2 == 0 ? negativeComponentIndex : positiveComponentIndex
for d in digits.reversed() {
components[0] <<= 3
components[1] <<= 3
components[componentIndex] += d // ERROR here
}
return components[positiveComponentIndex] - components[negativeComponentIndex]
}
}
The error is "Use of extraneous '&'", with the first "c" underlined. The error has a red-with-white-dot fix-it button. The suggestion is "Replace 'components' with ''". (In other words, the line would be "[componentIndex] += d", which leads to an "Left side of mutating operator has immutable type '[Int]'" error.) Neither the error nor the fix-it make any sense. Am I missing something legitimate? Or are these just beta problems?
(Replacing the custom Int4 with Int8 doesn't make a difference. The Int4 type, along with a UInt4 type, are defined in auxiliary files within the playground package.)
So, is this fixable? Or at least has a workaround? Note that I'm not explicitly calling this inout/& bug; it's being implicitly triggered by (I guess) the subscript-set.
I went back to Xcode 9.4 and realized I messed up.
components[componentIndex] += d
should be:
components[componentIndex] += T(d)
Since d is an Int4. It seems that there is no mixed-mode +=. Fixing that made the error go away. (That doesn't mean the (re)discovered bug isn't a problem.)
Anyway, I threw this method out and moved the code to an extended initializer for BinaryInteger. I feel like I should make a fail-able initializer version, but I don't think there's a general way to detect when my intermediate calculations would cause either a run-time error (i.e. crash) or roll off value bits to the sign bit and/or completely off the register.