RawRepresentable bug or intended?


(Adrian Zubarev) #1

struct B : RawRepresentable {
     
    let rawValue: Int
     
// init?(rawValue: Int) {
//
// self.rawValue = rawValue
// }
     
    static let c: B = B(rawValue: 0)
    static let d: B = B(rawValue: 1)
}
It seems to me that the memberwise initializer init(rawValue: Int) ignores the failable initializer init?(rawValue: Int) from RawRepresentable and is even able to satisfy RawRepresentable in that case.

···

--
Adrian Zubarev
Sent with Airmail


(Jordan Rose) #2

Yep, that's correct. A non-failable initializer is considered a valid subtype of a failable initializer, in that you can always provide the former where the latter is expected. The compiler doesn't handle all forms of subtyping when it does protocol conformance checks (and there are bugs on this), but it does handle this one.

Jordan

···

On Sep 28, 2016, at 23:00, Adrian Zubarev via swift-users <swift-users@swift.org> wrote:

struct B : RawRepresentable {
     
    let rawValue: Int
     
// init?(rawValue: Int) {
//
// self.rawValue = rawValue
// }
     
    static let c: B = B(rawValue: 0)
    static let d: B = B(rawValue: 1)
}
It seems to me that the memberwise initializer init(rawValue: Int) ignores the failable initializer init?(rawValue: Int) from RawRepresentable and is even able to satisfy RawRepresentable in that case.