import SwiftUI
// Why does this works
enum Blueprint {
case boeing747
case concorde
}
protocol Plane { }
struct Boeing747: Plane { }
struct Concorde: Plane { }
func factory(_ blueprint : Blueprint) -> some Plane {
return Boeing747()
}
// And this does not work ?
enum Screen {
case homepage
case setting
case loggin
}
struct HomePage: View { var body: some View { Text("Homepage") } }
struct Setting: View { var body: some View { Text("Setting") } }
struct Loggin: View { var body: some View { Text("Loggin") } }
func screenBuilder(_ screen : Screen) -> some View {
switch screen {
case .homepage:
return HomePage()
default: break
}
return Text("Not Implemented")
}
I don't understand why I have this error.
error: SwiftUIRouter.playground:37:6: error: function declares an opaque return type, but the return statements in its body do not have matching underlying types
func screenBuilder(_ screen : Screen) -> some View {
^
SwiftUIRouter.playground:40:12: note: return statement has underlying type 'HomePage'
return HomePage()
^
SwiftUIRouter.playground:43:10: note: return statement has underlying type 'Text'
return Text("Not Implemented")
Opaque result types only hide the type from the caller. The callee still has to honor the contract, which is that a function returning an opaque type always returns the same concrete type.
You can get around the bug by first create an variable and use that in the If/Else:
let homePage = HomePage() // <== work around the bug
let text = Text("Not Implemented")
@ViewBuilder
func screenBuilder(_ screen : Screen) -> some View {
if screen == .homepage: {
homePage // <== use this instead
} else {
text
}
}