swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.28k stars 10.33k forks source link

Specialized `init` is not available as implicit init in `@propertyWrapper`s #59505

Open b8591340 opened 2 years ago

b8591340 commented 2 years ago
import Foundation

@propertyWrapper
struct Wrap<Value> {
    var wrappedValue: Value
    init(wrappedValue: Value) {
        self.wrappedValue = wrappedValue
    }
}

extension Wrap where Value: ExpressibleByStringLiteral {
    init() {
        self.init(wrappedValue: "")
    }
}

class Object {
    @Wrap var value: String // error: Class 'Object' has no initializers
}
pyrtsa commented 1 year ago

Workaround: Move the extension into the original declaration body (SE-0267):

@propertyWrapper
struct Wrap<Value> {
    var wrappedValue: Value
    init(wrappedValue: Value) {
        self.wrappedValue = wrappedValue
    }

    // SE-0267
    init() where Value: ExpressibleByStringLiteral {
        self.init(wrappedValue: "")
    }
}

class Object {
    @Wrap var value: String // no error!
}