Error: 'Int' does not convert to '@lvalue Float'

Given the following function:

func greatestCommonDenominator(first: Int, second: Int) -> Int {
    return second == 0 ? first : greatestCommonDenominator(second, first % second)
}

And a structure in which there are the following things:

struct Fraction {
    var numerator: Int
    var denominator: Int

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

    // stuff
}

I get 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 kind? I don't have a float anywhere here. And the documentation seems to suggest that it /=should return Intas I am sharing two Ints. How to fix it?


APPENDIX: I came across this problem working inside the structure, however the problem seems to be reproducible anywhere.

let a = 10
a /= 5

This will cause the same problem. Even if we explicitly type aas Int:

let a: Int = 10
a /= 5

The same problems remain. Swift seems to think that the result of the statement /=between two Ints is Float.


EDIT: , a /= 5 . !

var a: Int = 4
var b: Int = 3
a /= b

a 3. . a let, a var, .

+4
1

, :

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

:

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

, , Objective-C ( RTFM), , . , :

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

.

/= . .


: .

, r, . r , ​​ . let , let. . , , , , rvalue , , const Objective-C.

+4

Source: https://habr.com/ru/post/1548182/


All Articles