Skip to content

Instantly share code, notes, and snippets.

@inamiy
Last active August 29, 2023 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inamiy/69f24a380f4bcd11c87d421e94c6f2f8 to your computer and use it in GitHub Desktop.
Save inamiy/69f24a380f4bcd11c87d421e94c6f2f8 to your computer and use it in GitHub Desktop.
[Swift] Recursive opaque type fails compile https://bsky.app/profile/inamiy.bsky.social/post/3k633r4kez72o
// Recursive opaque type fails compile because recursiveness can’t deterministically pick the right type to replace `some P`.
protocol P {}
struct Parent<Child: P>: P {
let children: [Child]
}
func recursive() -> some P {
// ERROR: Function opaque return type was inferred as 'Parent<some P>', which defines the opaque type in terms of itself
Parent(children: [recursive()])
}
// ERROR: Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
func recursive2<T: P>(_ x: T) -> some P {
return recursive2(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment