let collection : Any<Sequence where .Iterator.Element == String> = ...
要么
let collection : Sequence<where .Iterator.Element == String> = ...
这在Apple’s Generics Manifesto中被称为“广义存在”.(我认为)我真的需要这个用于许多用例和这个:
protocol ‘P’ can only be used as a generic constraint because
it has Self or associated type requirements.
使“第一个面向协议的语言”对我来说很难理解.缺乏这一点使我打击Swift的类型系统并创建不利的通用“抽象”类,其中应该有一个带有关联类型的协议.
protocol GenericclassDelegate : class { associatedtype ItemType func didDoThat(who : Genericclass<ItemType>) } class Genericclass<T> { weak var delegate : GenericclassDelegate<where .ItemType == T>? // can't do that func notify() { delegate?.didDoThat(who: self) } }
虽然我可以描述GenericclassDelegate协议,但我(当前在Swift 3中)不能拥有该类型的变量或常量(或任何符合限制的类型).
不要将此问题与How to use generic protocol as a variable type或Swift delegate protocol for generic class混淆,因为我的问题是:
>目前有关于将广义存在引入Swift的任何提议或讨论,计划是什么?如果不是,我该如何参与并影响到这一点?
>如果以这种方式设计Swift(使用关联类型,但没有广义存在),也许它意味着一些架构转变.我期望用什么代替委托模式?
附:当你在一个闭包中捕获委托的函数时,不建议使用类型擦除的thunk,这是非常错误和误导,我甚至称之为拐杖.
无意中发现了另一种解决方案,但我对此并不满意:
protocol GenericclassDelegate : class { associatedtype ItemType func didDoThat(who : Genericclass<ItemType,Self>) } class Genericclass<T,Delegate : GenericclassDelegate> where Delegate.ItemType == T { weak var delegate : Delegate? func notify() { delegate?.didDoThat(who: self) } init(_ delegate : Delegate?) { self.delegate = delegate } } // Delegates must be final classes,otherwise it does not compile // because I used Self in GenericclassDelegate final class GenericclassDelegateImp<T> : GenericclassDelegate { typealias ItemType = T func didDoThat(who: Genericclass<T,GenericclassDelegateImp>) { print(who) } } // Usage: var delegate = GenericclassDelegateImp<Int>() var genericclass = Genericclass<Int,GenericclassDelegateImp<Int>>(delegate)
解决方法
Are there any proposals or discussions currently present on introducing Generalized Existentials into Swift,what are the plans? If no,how can I participate and affect this?
这是一个常见的功能,并且已经有关于快速演化的初步设计工作.但此时,核心团队和社区正在关注影响功能的ABI稳定性,或者Lattner将其定义为“Swift 4 Phase 1”.
当第2阶段开始时,你肯定会听到更多关于它的信息.鉴于它的受欢迎程度,它有望成为Swift 4的一部分.
If Swift was designed that way (with Associated Types,but without Generalized Existentials),maybe it implies some architectural shift. What am I expected to replace delegation pattern with?
您可以使用类型擦除的包装器作为传递解决方案.通常,它利用动态分派和类的继承来擦除类型.
protocol Fancy { associatedtype Value var value: Value } struct FancyMatter<Value> { let value: Value } class AnyfancyboxBase<P: FancyProtocol>: Anyfancybox<P.Value> { let base: P override var value: P.Value { return base.value } init(_ base: P) { self.base = base } } class Anyfancybox<Value> { var value: Value { fatalError() } } var Box: Anyfancybox<Int> = AnyfancyboxBase(FancyMatter(1))
你可以看看how the Standard Library implements type-erased wrappers.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。