考虑以下因素:
protocol DataContainer {
typealias Data
}
protocol Foo {
typealias FooBar: DataContainer
typealias FooBaz: DataContainer
typealias Schema
}
final class FooClass<F: Foo where F.Schema == F.FooBar.Data, F.Schema == F.FooBaz.Data> {}
这个相当富有表现力 where
子句编译时不会出现神秘的错误消息:
Command failed due to signal: Abort trap 6
...
swift(4946,0x7fff777c9300) malloc: *** error for object 0x7fff5a5fbfe0: pointer being freed was not allocated
这对我来说完全没有意义。我已经确认,当我删除 where
子句约束时,错误就会消失,如下所示:
final class FooClass<F: Foo> {}
我知道这是在黑暗中进行的一次漫长的尝试,但是有人经历过吗?
如果是这样,您找到了解决方法吗?
<小时/>附录:如果 swift 允许在协议(protocol)
本身内部表达等式约束,而不是将约束泄漏到其实现者中,那么这种情况本来可以完全避免。引用this question .
最佳答案
好的。因此,经过几次强力调试后,我发现如果我对约束参数使用相同的 type
名称,它确实可以编译。
例如,
protocol Foo {
typealias FooBar
typealias FooBaz
typealias Data
}
final class FooClass<F: Foo where F.Data == F.FooBar.Data, F.Data == F.FooBaz.Data> {}
这将编译。请注意,我所做的只是将 Schema
更改为 Data
。这绝对是一个错误。
-编辑:并进行相应报告。
关于Swift: 'Abort trap: 6 - pointer being freed was not allocated' 带有类 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34036240/