[swift-evolution] Specified Protocol Conformances in Subclasses
Adrian Zubarev
adrian.zubarev at devandartist.com
Fri Mar 10 06:27:23 CST 2017
I kinda feel this is somehow related: http://discourse.natecook.com/t/pitch-return-a-subclass-for-a-protocol-method-without-the-need-for-an-associatedtype/2355
It would be really handy to be able to override any type A with another type B where the relationship is B : A.
--
Adrian Zubarev
Sent with Airmail
Am 10. März 2017 um 13:08:37, Rod Brown via swift-evolution (swift-evolution at swift.org) schrieb:
Hi everyone. I found something odd that seems baked into how Cocoa Touch does protocols, but Swift cannot model it:
@interface UIScrollView: UIView
@property (weak, nonatomic) id <UIScrollViewDelegate> delegate;
@end
@interface UITableView: UIScrollView
@property (weak, nonatomic) id <UITableViewDelegate> delegate;
@end
@protocol UITableViewDelegate: UIScrollViewDelegate
...
@end
Subclasses can further specify the conformance of a property’s protocol-conforming object to state a further type on that property. I tried to do something extremely similar today in Swift, and it failed, saying the protocols were different:
class SourceBar: UIScrollView {
override var delegate: SourceBarDelegate? {
get { return super.delegate as? SourceBarDelegate }
set { super.delegate = newValue }
}
}
@objc protocol SourceBarDelegate: UIScrollViewDelegate {
func foo()
}
Considering the fact that the protocol SourceBarDelegate conforms to UIScrollViewDelegate, I can’t see why this override should fail. Can anyone enlighten me as to why this is a limitation in the language?
Thanks,
Rod
_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170310/bbf1d15e/attachment.html>
More information about the swift-evolution
mailing list