Brynjar
Brynjar

Reputation: 1262

Swift is unable to type infer variable containing closure that returns Bool

I have a method, and it contains the following:

let downloadNextPagination = {
    if current.integerValue < amount.integerValue {
        if current.integerValue != amount.integerValue - 1 {
            return true
        }
    }
    return false
}

if downloadNextPagination() {
    // Do something
}

This code will not compile: Unable to infer closure return type in current context

Changing the definition of downloadNextPagination to

let downloadNextPagination: () -> Bool

fixes the issue. Question is: Why can Swift not work out the correct type of the closure here? All code paths return Bool, yet this can't be worked out? Is this a fundamental limitation / am I missing something in my understanding here, or is this simply a question of improving the compiler's type inference ability and something like this may well work come Swift 3?

Upvotes: 3

Views: 615

Answers (1)

Aleš Kocur
Aleš Kocur

Reputation: 1908

Swift can currently infer only single-return closures. You can rewrite your closure to:

let downloadNextPagination = { 
    return current.integerValue < amount.integerValue && current.integerValue != amount.integerValue - 1 
}

Upvotes: 0

Related Questions