Definite initialization feature

I don't quite understand this definite initialization feature from this pitch. Can somebody explain?

let bullet: String
if isRoot && (count == 0 || !willExpand) { bullet = "" }
else if count == 0 { bullet = "- " }
else if maxDepth <= 0 { bullet = "▹ " }
else { bullet = "▿ " }

If you forget "else" compiler will complain that "bullet" is not initialised. In other words compiler gives you a guarantee that "let bullet: String" is definitely initialised at some point (and definitely before it is used). Contrary to the case where you have, say, "var bullet: String!" (strictly speaking in that case the bullet is also initialised (to nil) which may not be expected by the following code).

3 Likes