I have a protocol named Foo and a struct named Bar. Bar conforms to Foo.
protocol Foo {}
struct Bar: Foo {}
Appending a Bar instance to an array of Bar works as expected.
var array = [Bar]()
array.append(Bar())
Now, I have a generic struct called Baz that's initialized with a type that conforms to Foo (e.g. Bar).
struct Baz<T: Foo> {
private(set) var array = [T]()
init() {
if T.self is Bar.Type {
// Error: Cannot invoke 'append' with an argument list of type (Bar)
array.append(Bar())
}
}
}
Attempting to append to the array results in the following error:
Cannot invoke 'append' with an argument list of type (Bar)
Why doesn't this work as expected?
As an aside, the use case is something like this:
let bazBarStack = Baz<Bar>().array
let bazQuxStack = Baz<Qux>().array
array.append(Bar() as T)?