I have a minimal prototype implementation of opaque result types in this pull request:
https://github.com/apple/swift/pull/21137
Limitations include:
- The opaque type is spelled
__opaque Protocol
or__opaque ProtocolA & ProtocolB [& ProtocolC...]
as placeholder spelling. - Opaque types can only appear as the return type of
func
declarations, not properties or closures. -
where
clause constraints on the opaque type are not yet supported, nor are conditional conformances on the opaque type. However, the protocols in the__opaque
type can be arbitrary protocols, with any number of associated type or Self type requirements.__opaque
types can in turn be inferred as associated types of protocol conformances. - The runtime and resilience aspects are not yet implemented. Changing the underlying type of a public opaque type will break ABI.
Some known issues:
- Because of the missing runtime support, I substitute out opaque types during SILGen in a rather hacky unprincipled way. It's likely that the optimizer, particularly generic specialization, may still cause crashes.
- The type checker currently crashes if there's a type error in the underlying type, such as if it does not conform to the required protocols.
- Diagnostics involving opaque types will print the type with a compiler-internal representation involving the original decl name and generic arguments, instead of anything human-understandable.
I asked CI to build a toolchain: https://ci.swift.org/job/swift-PR-toolchain-osx/170//artifact/branch-master/swift-PR-21137-170-osx.tar.gz I'd appreciate help kicking the tires on this. Thanks!