Consider the following code which just sets a predicate on init in a view which used to work fine with Swift 5.9

let predicate: Predicate<Transaction> = if let accountName {
    #Predicate<Transaction> { transaction in
        transaction.account?.name == accountName
    }
} else {
    #Predicate<Transaction> { _ in
        true
    }
}

This causes the following compile error in 5.10

Value pack expansion can only appear inside a function argument list or tuple element

I tried figuring out what has changed and came across a few discussions related to this error, but couldn't really figure out why this is now failing.

I had an inkling it had something to do with macro expansion and the if let expression. Whether that is correct or not, I did manage to fix my issue like so:

My code now builds and works fine now, but would be interested in understanding what changed in 5.10 to cause this and if maybe there is a more elegant way of handling it.

Note that swiftformat appears to automatically re-format this expression to my version.

2

There are 2 best solutions below

2
Joakim Danielson On BEST ANSWER

An alternative solution

var predicate: Predicate<Transaction> = Predicate.true

if let accountName {
    predicate = #Predicate<Transaction> { transaction in
        transaction.account?.name == accountName
    }
} 
0
wombat On

If you are stuck with this error, here is how I solved it

let predicate: Predicate<Transaction>

if let accountName {
    predicate = #Predicate<Transaction> { transaction in
        transaction.account?.name == accountName
    }
} else {
    predicate = #Predicate<Transaction> { _ in
        true
    }
}