DifferenceOfSquares test - @autoclosure errors

Hey all,

When I try to execute the tests for the Swift “Difference of Squares” exercise, I get errors from the test file for each and every test case:

/mnt/exercism-iteration/Tests/DifferenceOfSquaresTests/DifferenceOfSquaresTests.swift:9:34: error: add () to forward @autoclosure parameter
    XCTAssertEqual(1, Squares(1).squareOfSum)
                      ~~~~~~~~~~~^~~~~~~~~~~
                                            ()

Searching online shows other people getting this error when there was a change made with Swift 5.0 (https://github.com/dotintent/react-native-ble-plx/issues/455

Am I doing something wrong, or is there a problem with the tests in this exercise?

Thanks!

1 Like

Are you using the online editor or do you work through the cli?

I use the online editor.

Could you show the code you used?

Sure:

class Squares {
    // Write your code for the 'Difference Of Squares' exercise here.
    var value = 0
    
    init(_ val: Int) {
        self.value = val
    }

    func squareOfSum() -> Int {
        var sum = 0
        for i in 1...self.value {
            sum += i
        }
        return sum*sum
    }

    func sumOfSquares() -> Int {
        var total = 0
        for i in 1...self.value {
            total += i*i
        }
        return total
    }

    func differenceOfSquares() -> Int {
        squareOfSum() - sumOfSquares()
    }
}

So what the tests are looking for is a property and not a function. A property is a variabel. You can tell the difference by there not being any braces when being called.

That is not clear from the description of the exercise. I updated my code (to use properties and not functions) and it executes without problems now. Thanks for your help!

I have worked a bit on reducing the number of exercises using properties but there are quite a few still. In general, I think what is needed is an exercise explaining properties. I just havnt come that far. And the error message I think got a bit extra confusing since Swift recognized that you had a function with the same name, so it started saying that braces should be added in the test file, while what actually was happening was that there wasn’t a property with that name.