I'd like Swift to infer the typealias name for autocomplete, type hinting, error outputs, etc. For example, to simulate nested protocols:
// in Objc:
@protocol Parent_Child;
@protocol Parent <NSObject>
@property (nonatomic, readonly) id<Parent_Child> child;
@end
// in Swift:
extension Parent {
typealias Child = Parent_Child
}
var p: (any Parent)?
let c = p?.child // type is inferred as (any Parent_Child)? instead of (any Parent.Child)?
This example is an issue if the original Parent protocol is defined in Swift, too.
1 Like
We specifically don't preserve the sugar in this case (where you're accessing the type alias as a member of the protocol itself, rather than a conforming type or generic parameter) because it cannot be represented right now (swift/lib/Sema/TypeCheckType.cpp at main · swiftlang/swift · GitHub).
Please file an issue, I'd like to revisit type alias type representation at some point because it's a performance bottleneck, and fixing the performance issue would also allow the preservation of sugar in this case, too.
(However keep in mind that in general the compiler cannot and will not preserve sugar everywhere, it's a best-effort thing that sometimes works and sometimes doesn't, for various reasons.)
1 Like