LettersBa
LettersBa

Reputation: 767

Closures with shorthand not working

I have this closure here, That checks if the first value is greater than the second and if true returns a Boolean value:

var bloco12: (Int, Int -> Bool) -> Bool = {

    return $1($0)
}

println(bloco12(32, {$0 > 10}))

But I learned that there is another way to simplify this only using the symbol >, like this:

bloco12(32, >)

But this code doesn't work, why?

Upvotes: 0

Views: 52

Answers (1)

vacawama
vacawama

Reputation: 154523

Note that the > function takes two parameters and returns a Bool. Your example didn't work because you were trying to pass > to a parameter expecting a function that takes only one parameter. I've changed your example to show how you could just pass >:

var bloco12: (Int, Int, (Int, Int) -> Bool) -> Bool = {

    return $2($0, $1)
}

println(bloco12(32, 33, {$0 > $1}))  // prints "false"
println(bloco12(32, 33, >))          // prints "false"
println(bloco12(32, 33, <))          // prints "true"
println(bloco12(32, 33, ==))         // prints "false"

Upvotes: 2

Related Questions