Given the following function:
func greatestCommonDenominator(first: Int, second: Int) -> Int {
return second == 0 ? first : greatestCommonDenominator(second, first % second)
}
And a struct with the following stuff in it:
struct Fraction {
var numerator: Int
var denominator: Int
func reduce() {
let gcd = greatestCommonDenominator(numerator,denominator)
self.numerator /= gcd
self.denominator /= gcd
}
// stuff
}
I'm getting the following error:
error: 'Int' is not convertible to '@lvalue Float'
self.numerator /= gcd
^
error: 'Int' is not convertible to '@lvalue Float'
self.denominator /= gcd
^
'@lvalue Float'
?!?!? What? I don't have a Float anywhere in here. And the documentation seems to suggest that the /=
should return an Int
as I'm dividing two Int
s. How do I fix this?
ADDENDUM: I came across this problem working within a struct, however the problem seems reproducible anywhere.
let a = 10
a /= 5
This will produce the same problem. Even if we explicitly type a
as an Int
:
let a: Int = 10
a /= 5
The same problem remains. Swift seems to think the result of the /=
operator between two Ints is a Float.
EDIT: The problem with the addendum isn't actually that a /= 5
doesn't work. It does actually!
var a: Int = 4
var b: Int = 3
a /= b
Now a
is 3. The problem in the addendum was similar to the struct. In the addendum, a
was declared as a let
rather than a var
, and as such it is unassignable.
If you start by changing the function to the following you get a more helpful error message:
Now the error becomes:
What may not be immediately obvious to those of us who are coming from Objective-C (or didn't RTFM) is that by default, functions in structs are not allowed to change the properties of a struct. You can get around this by explicitly declaring the function as a mutating function:
The mutating word solves both errors.
In the case of
/=
, the error is quite cryptic and unhelpful. I'll be filing a bug report and encourage others to do so as well.EDIT: The real problem here has nothing to do with structs or compound assignment operators.
The problem here has to do with the fact that we're trying to assign to an rvalue that's unassignable. In the case of the struct, the rvalue was unassignable because the function was not declared as mutating. In the case of the
let
variable, it was unassignable because that's howlet
works. The error message is still misleading and confusing however. Rather than suggesting that there might be a type mismatch, the error should inform us that the rvalue is unassignable, just as it would if we tried to assign aconst
in Objective-C.