Last active
July 1, 2020 14:34
-
-
Save lukeredpath/a13f4701dc72550934074a64c45b542e to your computer and use it in GitHub Desktop.
Weird Swift 5.2 error - works in Swift 5.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// full definition omitted: | |
public struct ValidatorOf<Value, Error> { | |
public let validate: (Value) -> Validated<Value, Error> | |
} | |
extension ValidatorOf { | |
static func its<T>(_ transform: @escaping (Value) -> T, _ validator: ValidatorOf<T, Error>) -> Self { | |
validator.pullback(transform) | |
} | |
} | |
// @Validating is a property wrapper that can take an array or variadic list of validators (ValidatorOf) | |
// Works in Swift 5.2 and 5.3: | |
struct ValidatingContainer { | |
@Validating( | |
.its({ $0.first }, .isEqualTo(1)) | |
) | |
var numbers: [Int] = [] | |
} | |
// Works in Swift 5.3 but errors in Swift 5.2: | |
struct ValidatingContainer { | |
@Validating( | |
.its(\.first, .isEqualTo(1)) | |
) | |
var numbers: [Int] = [] | |
} | |
// Swift 5.2 errors: | |
// Struct declaration cannot close over value '$0' defined in outer scope | |
// Struct declaration cannot close over value '$kp$' defined in outer scope | |
// However, I can use the keypath form in a simple let in 5.2 without errors: | |
struct ValidatingContainer { | |
let arrayValidator: ValidatorOf<[Int], String> = .its(\.first, .isEqualTo(1)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment