ExpressibleByStringLiteral provides a secret initialiser?
Tuesday, 05 March 2024
struct Blob {
let value: String
}
let blob = Blob("string")
Missing argument label ‘value:’ in call
extension Blob: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
self.value = value
}
}
This turns out to be helpful to clarify a type.
struct Foo {
private let value: String
init(_ blob: Blob) {
self.value = "blob:" + blob.value
}
init(_ value: String) {
self.value = value
}
}
let foo1 = Foo("string")
let foo2 = Foo(Blob("string"))
This is not an initializer!
If we add .init to force the use of an initializer:
Blob.init("string")
This will provide a failure that there is no matches for the initializer:
No exact matches in call to initializer
So what is this syntax doing?