Skip to content

Instantly share code, notes, and snippets.

@eliperkins
Last active April 25, 2016 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliperkins/3c102bb804e7afa053a699a42cbc72dd to your computer and use it in GitHub Desktop.
Save eliperkins/3c102bb804e7afa053a699a42cbc72dd to your computer and use it in GitHub Desktop.
protocol Able {
init?(foo: String)
}
protocol Ible {
init(bar: Int) throws
}
extension Able where Self: Ible {
init?(foo: String) {
// The bug can still be reproduced without the contents of this method
// by just `return nil`.
guard let value = Int(foo) else { return nil }
guard let object = try? Self(bar: value) else {
return nil
}
self = object
}
}
struct Baz {
let buzz: Int
}
extension Baz: Able { }
extension Baz: Ible {
init(bar: Int) throws {
self.buzz = bar
}
}
let x = Baz(foo: "1") // Errors with: '(foo: String) -> Baz' is not convertible to '(foo: String) -> Baz?'
let y = Baz.init(foo: "1") // Works!
@eliperkins
Copy link
Author

Playground execution failed: MyPlayground.playground:31:23: error: '(foo: String) -> Baz' is not convertible to '(foo: String) -> Baz?'
        guard let x = Baz(foo: foo) else { return nil }

@mzaks
Copy link

mzaks commented Apr 21, 2016

Hm .. Baz implements Able and Ible. Seems like Baz(foo: "1") takes the init of Able which is init?
And I guess when you call Baz.init(foo: "1") it is explicit

You have a very strange construct there :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment