Closed 7ombie closed 2 months ago
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization#Setting-a-Default-Property-Value-with-a-Closure-or-Function
This code example uses empty parens after the closing brace to immediately invoke the closure that defines boardColors:
boardColors
struct Chessboard { let boardColors: [Bool] = { var temporaryBoard: [Bool] = [] var isBlack = false for i in 1...8 { for j in 1...8 { temporaryBoard.append(isBlack) isBlack = !isBlack } isBlack = !isBlack } return temporaryBoard }() func squareIsBlackAt(row: Int, column: Int) -> Bool { return boardColors[(row * 8) + column] } }
I've been trying to replace this syntax with do { ... } for a few years, and was told this would be available in Swift 6.
do { ... }
If I understand correctly, this should be legal (and preferred) in Swift 6:
struct Chessboard { let boardColors: [Bool] = do { var temporaryBoard: [Bool] = [] var isBlack = false for i in 1...8 { for j in 1...8 { temporaryBoard.append(isBlack) isBlack = !isBlack } isBlack = !isBlack } return temporaryBoard } func squareIsBlackAt(row: Int, column: Int) -> Bool { return boardColors[(row * 8) + column] } }
Just had a chance to check out Swift 6 Language Mode, and do-blocks are not implemented. I must have misunderstood something. Sorry for the noise.
Location
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization#Setting-a-Default-Property-Value-with-a-Closure-or-Function
Description
This code example uses empty parens after the closing brace to immediately invoke the closure that defines
boardColors
:I've been trying to replace this syntax with
do { ... }
for a few years, and was told this would be available in Swift 6.Correction
If I understand correctly, this should be legal (and preferred) in Swift 6: