I posted this issue in JIRA already, but wanted to ask here, if this is the expected behavior or a legit bug.
What I'm trying to do is have a static member (required by a protocol) that would return an array of elements of the associated type implemented by a struct using generics. Here's the snippet that demonstrates the implementation of the protocol requirement1:
// Protocol definitions
protocol PreviewProvider {
static var previewConfigurations: [ViewBuilder] { get }
}
protocol ConfigurableView: AnyView, PreviewProvider { ... }
And the implementation:
class Label: ConfigurableView { ... }
class CarouselView<Component: ConfigurableView>: ConfigurableView { ... }
extension Label {
static var previewConfigurations: [ViewBuilder] { [
Label.Configuration(text: "Regular size label", fontSize: 1),
Label.Configuration(text: "Bigger label", fontSize: 2),
Label.Configuration(text: "Even bigger label", fontSize: 3),
] }
}
extension CarouselView {
static var previewConfigurations: [ViewBuilder] { [] }
}
extension CarouselView where Component == Label {
static var previewConfigurations: [ViewBuilder] { [
CarouselView.Configuration(
items: [
Label.Configuration(text: "Hello", fontSize: 1),
Label.Configuration(text: "World", fontSize: 1),
])
] }
}
What I wanted to achieve was a way to enumerate an array of these types:
[Label.self, CarouselView<Label>.self]
.map { $0.previewConfigurations }
Which outputs:
[Label.Configuration, Label.Configuration, Label.Configuration],
[]
instead of:
[Label.Configuration, Label.Configuration, Label.Configuration],
[CarouselView<Label>.Configuration]
The previewConfigurations
static member I'm calling should've called the implementation found in the extension with the conditional conformance, but instead calls the generic one (which yields an empty array).
But if I call the static member directly, it will call the right one.
CarouselView<Label>.self.previewConfigurations
which will output an array of one element (as it should):
[CarouselView<Label>.Configuration]
But if I downcast it to the protocol type, it will call the member in the generic implementation:
(CarouselView<Label> as PreviewProvider.type).self.previewConfigurations
and output an empty array:
[]
What's the expected behavior?
1. The whole code example and a Swift Package where you can run the test is included in the JIRA ticket SR-14064.