jeudi 19 décembre 2019

How to make mutable copy of let parameter in generic function in Swift

I'm trying to make a generic test function in Swift that tests a function that mutates its argument. However, I get an error message that I can't find a way to solve:

public func testMutating<T: Comparable>(id: Int, input: T, output: T, testObj: (T) -> Any) {
    var inputMutating = input   // Make a mutable copy of input
    _ = testObj(&inputMutating) // Compiler error: Argument type 'inout T' does not conform to expected type 'Comparable'

    if (inputMutating == output) {
        print("Test \(id) PASSED :D")
    } else {
        print("Test \(id) FAILED :(")
        print("  Expected: \(output)")
        print("  Actual  : \(inputMutating)")
    }
}

Aucun commentaire:

Enregistrer un commentaire