I'm having a strange compile issue in Xcode 11/Swift 5.2 which compiles fine in Xcode 12/Swift 5.3 - I'm unsure if this is due to a bug in the previous version of Swift so was wondering if somebody could help because the compiler error is a bit...cryptic.
I'm just experimenting with a small high-level validations library and am trying to implement a property wrapper. I have a generic type, ValidatorOf<Value, Error>
which can define a validator on a particular type:
I have a wrapper type, which can be used as a property wrapper, called Validating<T>
which wraps an underlying value of type T
and also a validator of type ValidatorOf<T. String>
(the second generic parameter is just the error type):
I have a high-level validator that allows you to define a ValidatorOf<Value>
against a child of Value
of type T
, by providing a transform function (Value) -> T
, defined as:
extension ValidatorOf {
static func its<T>(_ transform: @escaping (Value) -> T, _ validator: ValidatorOf<T, Error>) -> Self {
validator.pullback(transform)
}
}
The pullback
function can be seen in the ValidatorOf.swift
file above.
So, on to the issue...first to demonstrate how the property wrapper works - I can define a validated string on a struct as follows:
struct Foo {
@Validating(.beginsWith("foo"))
var someString: String
}
This works fine. The problem arises when using the its
validator. The following works in both Swift 5.2 and 5.3:
struct Foo {
@Validating(.its({ $0.count }, .isAtLeast(6)))
var password: String
}
The problem arises with the key-path form of the above:
struct Foo {
@Validating(.its(\.count, .isAtLeast(6)))
var password: String
}
In Swift 5.3 this works fine, but in Swift 5.2 I get the errors:
Struct declaration cannot close over value '$0' defined in outer scope
Struct declaration cannot close over value '$kp$' defined in outer scope
Build output with this error can be seen here:
Thanks!