Swift 2: Can't implement a protocol with Self inside a function or a method
| Originator: | m.j.konieczny | ||
| Number: | rdar://21373555 | Date Originated: | 2015-06-14 |
| Status: | Open | Resolved: | |
| Product: | Product Version: | ||
| Classification: | Reproducible: |
First: thanks for Swift! I like it a lot.
In the process of migrating to Swift 2, I found a bug.
When a protocol has a Self requirement, I can't implement it inside a function or a method. See the attached source code. My use case is implementing a protocol in a test function: https://github.com/macoscope/SwiftyStateMachine/blob/350f9a0f5cdd5f3a11bcc6b6a6f03460b20716b8/StateMachineTests/StateMachineSpec.swift#L243-L253
Xcode 7.0 beta (7A120f)
OS X 10.10.3 (14D136)
attached test.swift:
// > xcrun swift --version
// Apple Swift version 2.0 (swiftlang-700.0.38.1 clang-700.0.53)
// Target: x86_64-apple-darwin14.3.0
protocol MyEnumProtocol {
static var allCases: [Self] { get }
}
// works on the top level
enum MyEnum: MyEnumProtocol {
case A
static var allCases: [MyEnum] { return [.A] }
}
class WorksInsideAClass {
enum MyOtherEnum: MyEnumProtocol {
case A
static var allCases: [MyOtherEnum] { return [.A] }
}
}
func doesntWorkInsideFunctions() {
// error: protocol 'MyEnumProtocol' can only be used as a generic
// constraint because it has Self or associated type requirements
enum MyYetAnotherEnum: MyEnumProtocol {
case A
static var allCases: [MyYetAnotherEnum] { return [.A] }
}
}
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!