Skip to content

Instantly share code, notes, and snippets.

@kylebshr
Last active October 9, 2020 00:53
Show Gist options
  • Save kylebshr/72e45453f29695566b0eded4f70d3b9c to your computer and use it in GitHub Desktop.
Save kylebshr/72e45453f29695566b0eded4f70d3b9c to your computer and use it in GitHub Desktop.
protocol MyProtocol {}
struct MyImplemention: MyProtocol {}
struct MyStruct {
func emptyView() -> some MyProtocol {
MyImplemention()
}
func someView<T>(_ view: T) -> some MyProtocol where T: MyProtocol {
MyImplemention()
}
// Error: Function declares an opaque return type, but the return statements in its body do not have matching underlying types
func emptyOrView<T>(view: T, showView: Bool) -> some MyProtocol where T: MyProtocol {
if showView {
return someView(view)
} else {
return emptyView()
}
}
}
@kylebshr
Copy link
Author

kylebshr commented Oct 9, 2020

This code is fairly nonsensical and doesn't do anything, but it should compile I think? If emptyOrView returns only one of someView(view) or emptyView() it works fine. Maybe I need to read more about opaque return types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment