Skip to content

Instantly share code, notes, and snippets.

@JensAyton
Created February 7, 2020 14:58
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 JensAyton/624d74861f06fb65abc3ba6e7138631e to your computer and use it in GitHub Desktop.
Save JensAyton/624d74861f06fb65abc3ba6e7138631e to your computer and use it in GitHub Desktop.
Surprising init resolution in Swift (5.1 and 5.2)
struct Test {}
extension Test: LosslessStringConvertible {
init?(_ description: String) {
print("unlabelled init")
}
var description: String { "Test" }
}
extension Test: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
print("stringliteral init")
}
}
let test = Test("Hello")
// Prints "stringliteral init"
// This happens if init(_:) is non-failable too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment