Reputation: 1291
I know this example is entirely useless and contrived, but I'm trying to better wrap my head around how values get handled in Swift and I don't understand why the following code returns an error.
var notSorted = sort([1,2,3,4]){true}
Upvotes: 1
Views: 272
Reputation: 108121
The second parameter of sort
has type (T, T) -> Bool
, but you are supplying a () -> Bool
, namely a closure with no parameters that returns true
.
Of course the two types don't match and you rightfully get a compile-time error.
Something like this will work:
var notSorted = sort([1,2,3,4]){_ in true}
By the way, that closure has the effect of reversing the array. If you want to perform a nop, you have to put false
.
Upvotes: 4