I have a strange problem with optional chaining and nil-coalescing in Swift. Can anyone explain why the following code won't compile:
class A<T> {
var val: T
var x: A<T>?
var y: A<T>?
init(t:T){
val = t
}
func test() -> [T] {
return (self.x?.test() ?? []) + [val] + (self.y?.test() ?? [])
}
}
But when writing
func test() -> [T] {
return (self.x?.test() ?? []) + ([val] + (self.y?.test() ?? []))
}
It does? The error says:
Cannot convert value of type '[T]?' to expected argument type '[_]?'
For me it looks a lot like a compiler bug.