Why there is parantheses after a computed property?

Why there is parantheses after this computed property?


let timeIntervalFormatter : DateComponentsFormatter = {
     let formatter = DateComponentsFormatter()
     formatter.uniqueStyle = .spellOut
     return formatter
}()

this piece of code from a book I use to learn swift from, but there was no expelenation regarding the paratheses?

Answered by Claude31 in 344419022

() means that you ask for immediate execution of the closure defined between bracket { }


without ( ), it is not executed immediately, but only when you call the var.

Accepted Answer

() means that you ask for immediate execution of the closure defined between bracket { }


without ( ), it is not executed immediately, but only when you call the var.

It's clearer if you think about these two cases:


let x = { … return DateComponentsFormatter() }
let y = { … return DateComponentsFormatter() } ()


The parentheses indicate which of two possible meanings you want. The type of "x" is a closure. The type of "y" is a DateComponentsFormatter.

Why there is parantheses after a computed property?
 
 
Q