It seem that Xcode preview compiler fail correctly parse generic and produce a "Failed to build" error:
Proposed example:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
import CoreBluetooth
import SwiftUI
/// Working in preview
/*
struct InitialView<S>: View where S: StringProtocol {
var body: some View {
NestedView()
}
struct NestedView: View {
var body: some View {
Text("Hello")
}
}
}
struct WorkingView: View {
var body: some View {
InitialView<String>()
}
}
*/
/// Not working in preview
struct NotWorking: View {
var body: some View {
InitialView<String>()
}
struct InitialView<S>: View where S: StringProtocol {
var body: some View {
NestedView()
}
struct NestedView: View {
var body: some View {
Text("Hello")
}
}
}
}
struct ContentView_PreviewProviders: PreviewProvider {
static var previews: some View {
NotWorking()
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^
investigating the SampleView.1.preview-thunk.swift from ~/Library/Developer/Xcode/DerivedData/.../ directory, I found that compiler translate the above code into pre-compiled stage using typealias without considering the Generic condition-
^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
extension NotWorking.InitialView.NestedView {
typealias InitialView = NotWorking.InitialView
typealias NestedView = NotWorking.InitialView.NestedView
@_dynamicReplacement(for: body) private var __preview__body: some View {
#sourceLocation(file: "/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift", line: 40)
Text(__designTimeString("#32812.[2].[1].[1].[0].property.[0].[0].arg[0].value", fallback: "Hello"))
#sourceLocation()
}
}
extension NotWorking.InitialView {
typealias InitialView = NotWorking.InitialView
typealias NestedView = NotWorking.InitialView.NestedView
@_dynamicReplacement(for: body) private var __preview__body: some View {
#sourceLocation(file: "/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift", line: 35)
NestedView()
#sourceLocation()
}
}
extension NotWorking {
@_dynamicReplacement(for: body) private var __preview__body: some View {
#sourceLocation(file: "/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift", line: 30)
InitialView<String>()
#sourceLocation()
}
}
...
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Should be considered a compiler bug or I missed something in my code?