且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swift Xcode 7 beta 5类型不能将其自身作为要求

更新时间:2022-11-20 17:15:25

我不知道为什么这应该是错误的.但这很容易(尽管很烦人):要声明另一种协议供Flier采用,并将其用作约束类型:

I have no idea why this should be wrong. But it's fairly easy (though annoying) to work around it: declare another protocol for Flier to adopt and use that as the constraining type:

protocol Superflier {}
protocol Flier : Superflier {
    typealias Other : Superflier
    func flockTogetherWith(f:Other)
}

这使代码真正受了折磨,但至少可以使它通过编译器.

It makes the code really tortured but at least it gets it past the compiler.

编辑:SevenTenEleven在开发论坛上的回复:

EDIT: Response on the dev forum from SevenTenEleven:

具有这种约束并不是天生不合理的,但是在某些情况下,它将导致编译器崩溃.我们决定暂时将其锁定.您使用第二种协议的解决方法是一种合理的方法.

Having this kind of constraint isn't inherently unreasonable, but in some cases it would lead to compiler crashes. We decided to lock down on this for now. Your workaround of using a second protocol is a reasonable one.

因此,如果感到遗憾,更改可能被认为是故意的,而我给出的解决方法是目前的解决方法.

So the change is probably to be regarded as deliberate if regrettable, and the workaround I've given is the way to go for now.