I suppose the motivating example isn't a huge problem, but it's definitely something that you'll have to look out for when implementing this.
The place where this transformation is not valid is in @_fixed_layout
types in resilient modules, where a property initializer need to only reference public
or @usableFromInline
things.
@_fixed_layout
public struct X {
public var x: (Int) -> Int = { $0 }
public var y: (Int) -> Int = X.y_initialValue()
static func y_initialValue() -> (Int) -> Int {
return { $0 }
}
}
Here, x
is fine because the closure is directly written. But y
calls something that's not @usableFromInline
, meaning:
test.swift:4:34: error: static method 'y_initialValue()' is internal and cannot be referenced from a property initializer in a '@_fixed_layout' type
public var y: (Int) -> Int = X.y_initialValue()
^
test.swift:5:15: note: static method 'y_initialValue()' is not '@usableFromInline' or public
static func y_initialValue() -> (Int) -> Int {
Also, if the static function is going to be @inlinable
or @_transparent
, it may need to show up in parseable interface files, which would mean it would need a name other than "varName.initialValue"