Syntax Explanation Please

on page 189, the code for structure seems to be a bit strange to me. After I checked the code to implement the tic-tac-toe game, there is a line of code that looks like this:

// Return true if all the board positions are filled or there is a winner
var isFinished: Bool {
return winner != nil || !board.contains { $0 == nil }
}

My question is whether isFinished assigned a function, closure or some other stuff? I am just confused about this syntax. Cuz if it is a closure shouldn’t it look like this with a assignment operator?

// Return true if all the board positions are filled or there is a winner
var isFinished: Bool = {
return winner != nil || !board.contains { $0 == nil }
}

@jinkazama isFinished is declared as a read-only computed property, so you define only a getter since the get clause is optional in this case. You can read more about computed properties in Chapter 11, “Properties”. Please let me know if you have any other questions or issues about the whole thing. Thank you!

1 Like

Thank You. After reading 11, it becomes clear.

This topic was automatically closed after 166 days. New replies are no longer allowed.