error: 'Int' is not convertible to '@l

2019-05-07 10:21发布

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 Ints. 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.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-07 10:42

If you start by changing the function to the following you get a more helpful error message:

func reduce() {
    let gcd = greatestCommonDenominator(numerator,denominator)
    self.numerator = self.numerator / gcd
    self.denominator = self.denominator / gcd
}

Now the error becomes:

error: cannot assign to 'numerator' in 'self'
        self.numerator = self.numerator / gcd
        ~~~~~~~~~~~~~~ ^

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:

mutating func reduce() {
    let gcd = greatestCommonDenominator(numerator,denominator)
    self.numerator /= gcd
    self.denominator /= gcd
}

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 how let 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 a const in Objective-C.

查看更多
登录 后发表回答